简体   繁体   中英

JS convert arrays into object then replace object key with property value

I need to convert an array into objects and then need to move one of object property value as a object property key name:

[
  {
    "description": "red",
    "type": "fruit",
    "item": "apple"
  },
  {
    "description": "yellow",
    "type":"fruit",
    "item": "banana"
  }
]

into

{
  "apple": {
    "description": "red",
    "type": "fruit"
  },
  "banana": {
    "description": "yellow",
    "type": "fruit"
  }
}

using Object.assign({}, ...arr) populates object's names into index, i need to change that index, thanks!

You can use Array#reduce to fold your array into an object. using destructuring you can pull out the values from the object and create a new object in your desired format for the output.

 const data = [ { "description": "red", "type": "fruit", "item": "apple" }, { "description": "yellow", "type":"fruit", "item": "banana" } ] console.log( data.reduce((accumulator, { item, description, type }) => ({ ...accumulator, [item]: { description, type } }), {}) ) 
 <script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script> 

You can use the function Array.prototype.reduce to build the desired output.

 var array = [ { "description": "red", "type": "fruit", "item": "apple" }, { "description": "yellow", "type":"fruit", "item": "banana" }], result = array.reduce((a, {description, type, item}) => (Object.assign(a, {[item]: {description, type}})), {}); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script> 

Unrelated: Nice console plugin from @synthet1c

Can be done with a single call to reduce .

 const data = [{ "description": "red", "type": "fruit", "item": "apple" }, { "description": "yellow", "type": "fruit", "item": "banana" } ] const regrouped = data.reduce((acc, { description, type, item }) => { acc[item] = { description, type } return acc }, {}); console.log(regrouped) 

One way is using a forEach on the array and build an object based on the properties:

 var arr = [{"description": "red", "type": "fruit", "item": "apple"},{"description":"yellow","type":"fruit","item": "banana"}] obj = {}; arr.forEach((o) => { obj[o.item] = o; delete o.item; }); console.log(obj) 

Other answers have covered the vanilla JS solution quite well, but if you can use lodash, you can do this by combining the methods keyBy , mapValues , and omit to produce a nice one-liner:

 const myArray = [{ "description": "red", "type": "fruit", "item": "apple" }, { "description": "yellow", "type": "fruit", "item": "banana" }]; const result = _.mapValues(_.keyBy(myArray, o => o.item), o => _.omit(o, "item")); console.log(result); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script> 

You can use reduce

 let arr=[ { "description": "red", "type": "fruit", "item": "apple" }, { "description": "yellow", "type":"fruit", "item": "banana" } ] const convert_to_object = (myarray) => myarray.reduce((o, i) => { o[i.item] = {description:i.description,type:i.type} return o }, {}) const peopleObject = convert_to_object(arr) console.log(peopleObject) 

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