Ritesh Kashyap
Ritesh Kashyap

Reputation: 394

Convert an Array into object type

I want to convert an array to object to specific object with key value pair.

[
    {
        "key": "out.of.stock",
        "value": "out of stock"
    },
    {
        "key": "buy.now",
        "value": "BUY NOW"
    },
    {
        "key": "notify.me",
        "value": "You'll receive an email"
    },
]

Output Required:

{
    labels :{
        "out.of.stock" : "out of stock",
        "buy.now" : "BUY NOW",
        "notify.me": "You'll receive an email"
        }
}

I tried using loadash (keyBy) but output is like:

{
   "out.of.stock": {
        "key": "out.of.stock",
        "value": "out of stock"
    },
    "buy.now":{
        "key": "buy.now",
        "value": "BUY NOW"
    },
    "notify.me": {
        "key": "notify.me",
        "value": "You'll receive an email"
    },
}

Upvotes: 0

Views: 83

Answers (7)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11011

Assuming the key/value order in the array, Try Object.fromEntries(data.map(Object.values))

const data = [
    {
        "key": "out.of.stock",
        "value": "out of stock"
    },
    {
        "key": "buy.now",
        "value": "BUY NOW"
    },
    {
        "key": "notify.me",
        "value": "You'll receive an email"
    },
]

const labels = Object.fromEntries(data.map(Object.values));

console.log({ labels })

Upvotes: 0

Parag Rudani
Parag Rudani

Reputation: 1

const data = [
    {
      key: "out.of.stock",
      value: "out of stock",
    },
    {
      key: "buy.now",
      value: "BUY NOW",
    },
    {
      key: "notify.me",
      value: "You'll receive an email",
    },
  ];

  const obj = { label: {} };

  Object.values(data).map(({ key, value }) => {
    return (obj.label[key] = value);
  });


console.log(obj)

Upvotes: 0

Ran Turner
Ran Turner

Reputation: 18116

I suggest using the forEach() method which will execute a provided function once for each array element. in that function you'll do the assignment for the new object by applying the key with the correlative value under the labels property object.

const data = [
    {
        "key": "out.of.stock",
        "value": "out of stock"
    },
    {
        "key": "buy.now",
        "value": "BUY NOW"
    },
    {
        "key": "notify.me",
        "value": "You'll receive an email"
    },
];

let obj = {"labels" :{}};
data.forEach(x => obj.labels[x.key] = x.value);

console.log(obj);

Upvotes: 0

dave
dave

Reputation: 64695

I would just do Object.fromEntries(data.map(({key, value}) => [key, value]))

const data = [
    {
        "key": "out.of.stock",
        "value": "out of stock"
    },
    {
        "key": "buy.now",
        "value": "BUY NOW"
    },
    {
        "key": "notify.me",
        "value": "You'll receive an email"
    },
]

console.log(
    Object.fromEntries(data.map(({key, value}) => [key, value]))
)

Upvotes: 0

nazlikrmn
nazlikrmn

Reputation: 59

let a=data.map((x)=>{
   let obj={}
   obj[x.key]=x.value
   return obj
})

you can use map function first and then keyby

Upvotes: 0

Adam Orłowski
Adam Orłowski

Reputation: 4484

const data = [
  {
    "key": "out.of.stock",
    "value": "out of stock"
  },
  {
    "key": "buy.now",
    "value": "BUY NOW"
  },
  {
    "key": "notify.me",
    "value": "You'll receive an email"
  },
]

const result = data.reduce((acc, next) => { 
  acc.labels[next.key] = next.value
  return acc
}, { labels: {} })

console.log(result)

Upvotes: 2

Liftoff
Liftoff

Reputation: 25412

A simple for each will do the trick.

    var obj = {labels:{}};
    arr.forEach(e => {
      obj.labels[e.key] = e.value;
    });

var arr = [
    {
        "key": "out.of.stock",
        "value": "out of stock"
    },
    {
        "key": "buy.now",
        "value": "BUY NOW"
    },
    {
        "key": "notify.me",
        "value": "You'll receive an email"
    },
];

var obj = {labels:{}};
arr.forEach(e => {
  obj.labels[e.key] = e.value;
});

console.log(obj);

Upvotes: 0

Related Questions