0

I have an array like this:

const categories = ["Action", "Comedy", "Thriller", "Drama"]

And I would like to convert it to this format:

const displayedCategories = [{"type": "Action", "isDisplayed": true},{"type": "Comedy", "isDisplayed": true},{"type": "Thriller", "isDisplayed": true},{"type": "Drama", "isDisplayed": true}]

Any advice ? :)

2
  • 3
    What have you tried so far? Commented Jan 12, 2020 at 17:59
  • 1
    For a future reference check out how to ask a good question stackoverflow.com/help/how-to-ask. @AndrewL64 is right, you should provide at least what you have tried, otherwise you're just asking others to solve the problem for you. Commented Jan 12, 2020 at 18:02

5 Answers 5

0

You can do so by using map:

const categories = ["Action", "Comedy", "Thriller", "Drama"];

const newCategories = categories.map(category => ({
  type: category,
  isDisplayed: true
}));

console.log(newCategories);

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

Comments

0

You can do so with the map method:

const displayedCategories = categories.map(function(category) {
   return { type: category, isDisplayed: true };
});

Comments

0

Maybe like This:

const categories = ["Action", "Comedy", "Thriller", "Drama"];
var out = [];
for(var key in categories){
  out.push({"type": categories[key], "isDisplayed": true});
}
console.log(out);

Comments

0

use array.prototype.map to convert the array into an object

categories = catergories.map(val => {
  var obj =  {
    type:val
    //more properties go here
  }
  return obj
}

Comments

0

const categories = ["Action", "Comedy", "Thriller", "Drama"]
const displayedCategories = categories.map(category =>  { return {"type": category, "isDisplayed": true} })
console.log('displayedCategories :', displayedCategories);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.