简体   繁体   中英

Converting an array of objects in to a single object

Have an array which contains a no of json .

[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]

Note that each Json have same key . I want to convert this array into a single json like

{linkValue1:"value1",linkValue2:"value2",linkValue3:"value3",linkValue4:"value4",linkValue5:"value5"}

one thing i also need to know . my array is also inside a json how i get this arry from that json ?

My initial json is

{name:"value",age:"value",linkValue:[[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]
]}

I'm expcting my final json look like :

{name:"value",age:"value",linkValue1:"value1",linkValue2:"value2",linkValue3:"value3",linkValue4:"value4",linkValue5:"value5"}

can anyone please help me

Use Array.forEach and add properties to an empty object:

let source = {name:"value",age:"value",linkValue:[[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]]};

// Copy the array in a variable
let yourArray = source.linkValue[0];

// Delete the original array in the source object 
delete source.linkValue;

yourArray.forEach((item, index) => {
    source["linkValue" + (index + 1)] = item.linkValue
});

console.log(source); // Should have what you want
[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]

This is javascript Array contains multiple javascript object.

{linkValue1:"value1",linkValue2:"value2",linkValue3:"value3",linkValue4:"value4",linkValue5:"value5"}

If you need structure like this,Then define a single javascript object and add linkvalue1,linkvalue2 etc. as a member of that object and then add it to javascript Array.

Solution with reduce

// HELPER FUNCTIONS

// pick properties from object with default value
function pick (props, sourceObj, defaultValue) {
  return props.reduce(
    (obj, prop) =>
      Object.assign(obj, { [prop]: sourceObj[prop] || defaultValue }),
    {}
  )
}

// get property value or default
function propOr (propName, obj, defaultValue) {
  return obj[propName] || defaultValue
}

// flatten nested array
function flattern (nestedArray) {
  return nestedArray.reduce((flat, innerArray) => flat.concat(innerArray), [])
}

// LINKS BUILDER based on REDUCE
function buildLinks (linksArray) {
  return linksArray.reduce((accumulator, item, index) => {
    accumulator['linkValue' + (index + 1)] = item.linkValue
    return accumulator
  }, {})
}


// TRANSFORMATION FUNCTION - takes json and produce required output
function transform(json) {
  return Object.assign(
    {}, 
    pick(['name', 'age'], json, null), 
    buildLinks(flattern(propOr('linkValue', json, [])))
  )
}

// EXECUTION
const result = transform(source)

PS> You can use libraries like Lodash or Ramda and replace helper functions defined by me with ones from library

Using reduce API,

let targetObj = srcArr.reduce((accumulator, value, index)=>{
   accumulator['linkValue'+(index+1)] = value.linkValue;
   return accumulator;
}, {});

试试看。

myObj.linkValue = myObj.linkValue.map((obj, index) => ({ [`linkValue${index + 1}`]: obj.linkValue }))

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