-1

I have an Object

let data =  {
        a: 1,
        b: 2,
        c: {
            abc: "ak",
            bcd: "gh",
            cfv: "ht"
        }
    }

then I have variables which I need to show with these object value

   let abc = "first 1", bcd="sec2", cfv="third3" , def="fourth 4", tdf = "fifth 5";

Now the Object will come in API call it can be any of these variable.

How can I match the variable name with the object data.c.(object key) and concatinate their value.

for example the output should be As we have (abc, bcd, cfv) as our object key then the output would be

first 1ak ==> that is the value of (abc + data.c["abc"])
sec2gh    ==> that is the value of (bcd + data.c["bcd"])
third3ht  ==> that is the value of (cfv + data.c["cfv"])

I tried using Object.keys() method so from this method we will get the object keys in array then how can I match with the variable name - Object.keys(data.c); ==> ["abc", "bcd", "cfv"] (After this how can I proceed to match the variable and show their values?)

Shall I loop throught the object that (data.c)?

Please help me giving some ideas to achieve this implementation. thank you

0

1 Answer 1

1

If it's possible for you to amend the format of your abc, bcd etc. variables to be the properties in an object, then this problem becomes trivial. You can use flatMap() to create a new array of the output values by linking the properties of the two target objects, like this:

let values = {
  abc: "first 1",
  bcd: "sec2",
  cfv: "third3",
  def: "fourth 4",
  tdf: "fifth 5"
}

let data = {
  a: 1,
  b: 2,
  c: {
    abc: "ak",
    bcd: "gh",
    cfv: "ht"
  }
}

let output = Object.keys(values).flatMap(k => data.c.hasOwnProperty(k) ? values[k] + data.c[k] : []);
console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.