-1

Hi everyone I have object of object and I just want to combine data inside inner object in the form of array is there any way to that

input data

  let data = {
      
      ok123b:{
        name:'shanu'
    },
       of123b:{
        name:'rahul'
    },
       og1453jdfk:{
        name:'ak'
    },
       ok1kjjdde23b:{
        name:'man'
    }
      
      
    }
    
    let arrayOfData  =[data.ok123b.name,data.of123b.name,data.og1453jdfk.name,data.ok1kjjdde23b.name]
    console.log(arrayOfData);
    
    //this is hard coded i want dynamic code to convert data in the form of array

expected output

output  = ['shanu','rahul','ak','man'];
  • 1
    `Object.values(data).map(o => o.name)` (and please don't add unrelated tags) –  May 02 '22 at 07:05
  • Does this answer your question? [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – satoshisquash May 02 '22 at 07:42

2 Answers2

0

There are several ways you can achieve this. I have updated my answer to include these.

Solution 1 (For loop and push() method)

You can use a simple for loop and push (add) each object's name to the array.

let data = {
  ok123b:{name: 'shanu'}, 
  of123b:{name: 'rahul'}, 
  og1453jdfk:{name:'ak'}, 
  ok1kjjdde23b:{name: 'man'}
}
let arrayOfData = [];
for(item in data) {
  arrayOfData.push(data[item]['name'])
}
console.log(arrayOfData);

Solution 2 (Using Object.values() and map() method)

let data = {
  ok123b:{name: 'shanu'}, 
  of123b:{name: 'rahul'}, 
  og1453jdfk:{name:'ak'}, 
  ok1kjjdde23b:{name: 'man'}
}
let r = Object.values(data);
let result = r.map((element) => element.name)
console.log(result);

Solution 3 (One liner using Object.entries() and map() method)

let data = {
  ok123b:{name: 'shanu'}, 
  of123b:{name: 'rahul'}, 
  og1453jdfk:{name:'ak'}, 
  ok1kjjdde23b:{name: 'man'}
}
let result = Object.entries(data).map(a=>a[1].name)
console.log(result);

However as @Chris G mentioned, this is a very common question so you should probably browse similar questions such as:

Amit
  • 1,018
  • 1
  • 8
  • 23
  • If the answer to a question is easy and obvious to you, the question shouldn't exist in the first place. Please flag the question as the duplicate it is instead of posting an answer. SO works more like a wiki, not a chat forum. –  May 02 '22 at 07:06
  • Sorry about that @ChrisG, I'll flag the question now, should I delete my answer as well then? I thought it might be useful for people who had the same question – satoshisquash May 02 '22 at 07:09
  • Yes, feel free to do that, thanks :) –  May 02 '22 at 07:12
  • So it doesn't make sense to leave the answer up in case others have a similar confusion about something like this? – satoshisquash May 02 '22 at 07:13
  • Look at the question title. How likely do you think somebody with a similar (really basic) problem will find this? Also, again, this is a duplicate. Every easy question is, and about a dozen questions about restructuring arrays or objects are posted here like each hour. They all should be deleted, not answered. This website's purpose isn't that beginners who post zero effort questions get their easy stuff answered by pros for free. –  May 02 '22 at 07:17
  • Is this the duplicate then: https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically? It's not the exact same question though that was my only concern – satoshisquash May 02 '22 at 07:29
  • Ok I'll delete it once you see this message, thanks for all the help @ChrisG, and sorry for adding to the confusion – satoshisquash May 02 '22 at 07:35
  • 1
    No problem, and thanks for understanding my concerns instead of reacting badly :) –  May 02 '22 at 07:44
  • @ChrisG I understand that this question should be deleted but in the mean time while we wait for it to be deleted is there any harm in leaving my answer up since there are other answer as well? And once it's deleted will my answer be deleted anyways? – satoshisquash May 02 '22 at 07:49
-1

hi everyone if any one have same problem here is the solution

let data = {
  
  ok123b:{
    name:'shanu'
},
   of123b:{
    name:'rahul'
},
   og1453jdfk:{
    name:'ak'
},
   ok1kjjdde23b:{
    name:'man'
}
  
  
}
let r=Object.values(data);


let result =r.map((el) =>el.name)
console.log(result);
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Rando Hinn May 02 '22 at 10:36