Nightcrawler
Nightcrawler

Reputation: 1051

How to group array of object by key value pairs using javaScript?

I just started learning JavaScript, I have this type of array, how I can turn this array of objects into key-value pairs like below, Any source and reference is acceptable.

Sample Array:

[
  {Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1},
  {Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1}
]

Expected Result:

{
  "6d7e75e6-c58b-11e7-95-ac162d77eceb":1, 
  "6d2e75e6-c58b-11e7-95-ac162d77eceb":1
}

Upvotes: 0

Views: 526

Answers (6)

pilchard
pilchard

Reputation: 12911

Using Object.fromEntries()

const
  array = [{ Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1 }, { Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1 }],

  object = Object.fromEntries(array.map(({ Id, qty }) => [Id, qty]));

console.log(object);

or, for some fragile novelty...

const
  array = [{ Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1 }, { Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1 }],
  
  object = Object.fromEntries(array.map(Object.values));

console.log(object);

Upvotes: 0

mk23
mk23

Reputation: 1403

You can achieve the desired result with below code

//input array
const arrList = [
  {Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1},
  {Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1}
]

function findArray(arr) {
     //define a new array to store Id's
     let newArray = [];
     //iterate through array items
     arr.forEach(item => {
         newArray.push(item.Id);
    });

   return newArray;
}

//call findArray function to get desired output
console.log(findArray(arrList));

Upvotes: 0

Majed Badawi
Majed Badawi

Reputation: 28404

Using Array.prototype.Reduce:

const arr = [{Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1},{Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1}];

const result = arr.reduce((acc, { Id, qty }) => ({ ...acc, [Id]: qty }), {});

console.log(result);

Upvotes: 1

DecPK
DecPK

Reputation: 25398

You can easily achieve this result using forEach in a single line of code.

const arr = [
  { Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1 },
  { Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1 },
];

const result = {};

arr.forEach(({ Id, qty }) => (result[Id] = qty));

console.log(result);

Upvotes: 0

dale landry
dale landry

Reputation: 8600

You can use a loop and add the item.Id as the key and the item.qty as the value in an empty object.

let arr = [{Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1},{Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1}]

let obj = {}
arr.forEach(item => {   
   obj[item.Id] = item.qty   
})

console.log(obj)

Upvotes: 0

Ariel
Ariel

Reputation: 1436

Another approach, a little more beginner friendly.

const arr = [
  {Id: "6d7e75e6-c58b-11e7-95-ac162d77eceb", qty: 1},
  {Id: "6d2e75e6-c58b-11e7-95-ac162d77eceb", qty: 1}
];

const newObject = {}; // empty object

// loop over each element of the array
arr.forEach(element => {
  // the key is the element identifier (Id) and the value is the element quantity (qty)
  newObject[element.Id] = element.qty;
});

Upvotes: 0

Related Questions