0

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!

1
  • 1
    Just iterate over that array and place entries into a new object. Commented Apr 11, 2018 at 23:01

6 Answers 6

3

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>

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

4 Comments

Love that plugin!! (:
@Ele cheers, I wasn't happy with the stackoverflow console so made a simple one to help with presenting the results.
Some upvoted answers cuz you've shared with us that plugin!!
Cheers guys, feel free to use / fork it. it has an assertion library based on jasmine's api built in if you need it, there is examples at the bottom of the codepen, just remove the .js to see the pen.
2

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

Comments

0

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)

Comments

0

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)

Comments

0

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/[email protected]/lodash.min.js"></script>

Comments

0

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)

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.