0

I need to remove the star key from the object and output should be look like output variable. Can someone tell how to implement the removeStar function?

let input = {
  "p": {
    "pk1": "pv1",
    "pk2": "pv2",
    "c": {
      "*": {
        "ck1": "cv1",
        "ck2": "cv2"
      }
    }
  }
}

function removeStar(input) {
  // Suggest implementation of this function.
}

let expectedOutput = {
  "p": {
    "pk1": "pv1",
    "pk2": "pv2",
    "c": {
      "ck1": "cv1",
      "ck2": "cv2"
    }
  }
};

console.log(removeStar(input));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
nikhil
  • 823
  • 1
  • 7
  • 19

1 Answers1

0

I hope I could help you

let input = {
            "p": {
                "pk1": "pv1",
                "pk2": "pv2",
                "c": {
                    "*": {
                        "ck1": "cv1",
                        "ck2": "cv2"
                    }
                }
            }
        }

        function removeStar(input) {
            for (const i in input.p) {
                if (typeof input.p[i] === 'object') {
                    for (const x in input.p[i]) {
                        if (x === '*') {
                            input.p[i] = input.p[i][x];
                        }
                    }
                }
            }
            console.log(input)
        }

        removeStar(input)
davood beheshti
  • 213
  • 1
  • 11