简体   繁体   中英

JavaScript - Flatten Array of Nested Objects

I am working with an array of objects and need to flatten and re-assign the data set into an array of single objects using the object values as the new object keys.

Incoming Data:

results = [
  {
    surveyValues: [
      { id: 135, name: 'First Name', value: 'Jim' },
      { id: 136, name: 'Last Name', value: 'Jones' },
      { id: 137, name: 'City', value: 'Fredsburg' },
    ],
  },
  {
    surveyValues: [
      { id: 135, name: 'First Name', value: 'Greg' },
      { id: 136, name: 'Last Name', value: 'Jones' },
      { id: 137, name: 'City', value: 'Waverly' },
    ],
  },
];

Desired Result:

output = [
  {
    id: 1, FirstName: 'Jim', LastName: 'Jones', City: 'Fredsburg',
  },
  {
    id: 2, FirstName: 'Greg', LastName: 'Jones', City: 'Waverly',
  },
];

My current attempt gets me to placing the updated key/value pairs in the correct order, but creates a huge array of single objects.

Current Code:

const results = [];

surveyResults.map(s => s.surveyValues)
  .forEach(s => Object.entries(s)
  .forEach(([key, value]) => {
    Object.entries(value)
      .forEach(([k, v]) => {
        if (k === 'name') {
          results.push({
            id: value.id,
            [v]: value.value.toString(),
          });
        }
    });
}));

Current Code Output:

[
  {
    id: 135, FirstName: 'Jim',
  },
  {
    id: 136, LastName: 'Jones',
  },
  {
    id: 137, City: 'Fredsburg',
  },
  {
    id: 135, FirstName: 'Greg',
  },
  {
    id: 136, LastName: 'Jones',
  },
  {
    id: 137, City: 'Waverly',
  },
]

What am I missing and why is my code not created the new arrays of objects as desired?

You may use map and reduce to accomplish this. I'm also accessing an index i in the map function's callback to set the id property and string.replace() to strip the space from the FirstName and LastName keys.

 const results = [ { surveyValues: [ { id: 135, name: 'First Name', value: 'Jim' }, { id: 136, name: 'Last Name', value: 'Jones' }, { id: 137, name: 'City', value: 'Fredsburg' }, ], }, { surveyValues: [ { id: 135, name: 'First Name', value: 'Greg' }, { id: 136, name: 'Last Name', value: 'Jones' }, { id: 137, name: 'City', value: 'Waverly' }, ], }, ]; const result = results.map((e, i) => e.surveyValues.reduce((a, e) => { a[e.name.replace(" ", "")] = e.value; return a; }, {id: i + 1}) ); console.log(result); 

Create each object in the outer map() loop, and add properties to it in the inner forEach() .

 surveyResults = [ { surveyValues: [ { id: 135, name: 'First Name', value: 'Jim' }, { id: 136, name: 'Last Name', value: 'Jones' }, { id: 137, name: 'City', value: 'Fredsburg' }, ], }, { surveyValues: [ { id: 135, name: 'First Name', value: 'Greg' }, { id: 136, name: 'Last Name', value: 'Jones' }, { id: 137, name: 'City', value: 'Waverly' }, ], }, ]; const results = surveyResults.map((s, i) => { let v = s.surveyValues; let obj = {id: i}; v.forEach(item => obj[item.name.replace(/\\s+/g, "")] = item.value.toString()); return obj; }); console.log(results); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM