0

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"
    },
}
Andy
  • 61,948
  • 13
  • 68
  • 95
Ritesh Kashyap
  • 364
  • 1
  • 16

7 Answers7

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

Adam Orłowski
  • 4,268
  • 24
  • 33
0

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);
Liftoff
  • 24,717
  • 13
  • 66
  • 119
0
let a=data.map((x)=>{
   let obj={}
   obj[x.key]=x.value
   return obj
})

you can use map function first and then keyby

nazlikrmn
  • 59
  • 5
0

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]))
)
dave
  • 62,300
  • 5
  • 72
  • 93
0

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);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
0

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)
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
0

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 })
Siva K V
  • 10,561
  • 2
  • 16
  • 29