0

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
}
mk23
  • 1,257
  • 1
  • 12
  • 25
Nightcrawler
  • 943
  • 1
  • 11
  • 32
  • Does this answer your question? [How to concatenate properties from multiple JavaScript objects](https://stackoverflow.com/questions/2454295/how-to-concatenate-properties-from-multiple-javascript-objects) – pilchard Jun 23 '21 at 23:22

6 Answers6

1

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);
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

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;
});
Ariel
  • 1,366
  • 7
  • 16
0

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)
dale landry
  • 7,831
  • 2
  • 16
  • 28
0

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);
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

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));
mk23
  • 1,257
  • 1
  • 12
  • 25
0

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);
pilchard
  • 12,414
  • 5
  • 11
  • 23