-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

6
  • 3
    arabicLang: false as an element won't work (it's a syntax error), do you want this to be an object: {arabicLang: false}? Commented Feb 7, 2021 at 9: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}] Commented Feb 7, 2021 at 9:14
  • All object keys in Javascript are strings, the quotes are not needed for convenience when writing object literals. Commented Feb 7, 2021 at 9:16
  • You could use Object.entries() and flat the result but the result will be comma seperated and not semicolon comma semicolon comma Commented Feb 7, 2021 at 9: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. Commented Feb 7, 2021 at 9:25

2 Answers 2

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

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

Comments

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

3 Comments

Just Object.entries will produce the wanted result.
Object.entries will produce like [[key1, val1], [key2, val2]] right? here if we need it like array of objects.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.