-2

I want to convert an object like this:

let obj = {
arabicLang: false,
cancelVisitEmailAlert: false,
canselVisitSmsAlert: false
}

into an array of key-value pairs like this:

[
"arabicLang": false,
"cancelVisitEmailAlert": false,
"canselVisitSmsAlert": false
]

I read all questions in StackOverflow but none of them is my case

i try this but it return key and value in string:

    let data = [];
     for (const [key, value] of Object.entries(obj)) {
       data.push(createData(key, value));
     }

function createData(key, value) {
    return key + ":" + value;
  }

and also try this:

    let arr = Array.of(obj)
      console.log(arr) 
      /* output is 
      [{
    arabicLang: false,
    cancelVisitEmailAlert: false,
    canselVisitSmsAlert: false
    }]

*/

it keeps the object container

  • 3
    `arabicLang: false` as an element won't work (it's a syntax error), do you want this to be an object: `{arabicLang: false}`? – Nick Parsons Feb 07 '21 at 09:13
  • I think that you cannot create an `Array` like this, if you wanna create an `Array` of `objects` use `Object.entries(obj)` that will result in `[{a: 1}, {b:2}]` – AdriSolid Feb 07 '21 at 09:14
  • All object keys in Javascript are strings, the quotes are not needed for convenience when writing object literals. – spirift Feb 07 '21 at 09:16
  • You could use Object.entries() and flat the result but the result will be comma seperated and not `semicolon comma semicolon comma` – Aalexander Feb 07 '21 at 09:20
  • 1
    Since `[arabicLang: false]` is invalid syntax we can't really know what you expect it to do. Why do you want an array instead of an object? How do you plan to use that value? Maybe that helps understand what you are trying to achieve. – Felix Kling Feb 07 '21 at 09:25
  • Why would you want to do this? I can't think of any use case where this is a good idea. – Kielstra Feb 07 '21 at 09:47

2 Answers2

1

You can use .reduce or .map like:

let obj = {
    arabicLang: false,
    cancelVisitEmailAlert: false,
    canselVisitSmsAlert: false
};

// Using .reduce
const result = Object.entries(obj).reduce((acc, [key, value]) => {
  acc[key] ??= {[key]: value};
  return acc;
}, {});
console.log(Object.values(result));

// Using .map
const result2 = Object.entries(obj).map(([key, value]) => ({[key]: value}));
console.log(result2);
Mai Phuong
  • 111
  • 2
0

Only thing instead of createData method, just use object literal to simplify.

let obj = {
  arabicLang: false,
  cancelVisitEmailAlert: false,
  canselVisitSmsAlert: false,
};

let data = [];
for (const [key, value] of Object.entries(obj)) {
  data.push({ [key]: value });
}

console.log(data);
Siva K V
  • 10,561
  • 2
  • 16
  • 29
  • Just `Object.entries` will produce the wanted result. – trincot Feb 07 '21 at 09:47
  • Object.entries will produce like [[key1, val1], [key2, val2]] right? here if we need it like array of objects. – Siva K V Feb 07 '21 at 09:52
  • Probably, but the OP's notation is invalid, and makes it confusing. To me an *"array of key-value pairs"* is what `Object.entries` returns. – trincot Feb 07 '21 at 09:53