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
}
1

6 Answers 6

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);

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

Comments

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;
});

Comments

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)

Comments

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);

Comments

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));

Comments

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);

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.