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"
    },
}
4
  • use Array.reduce, Commented Jun 27, 2021 at 8:33
  • @Nur this is wrong, he wanted pair to by under labels Commented Jun 27, 2021 at 8:34
  • 2
    @Nur Is that why you gave all answers a minus because you think differently? 👀 Commented Jun 27, 2021 at 8:36
  • 1
    It is duplicated here. stackoverflow.com/q/42974735/14032355 Commented Jun 27, 2021 at 8:37

7 Answers 7

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)

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

1 Comment

How is that a minus 🤷‍♂️
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);

Comments

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

you can use map function first and then keyby

Comments

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

Comments

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

Comments

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)

Comments

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

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.