karo-s
karo-s

Reputation: 414

How to make array of objects from two another arrays?

I have two arrays.

let arr1 = ['dog', 'cat', 'mouse']
let arr2 = ['dog']

How to create another third array from the result?

let arr3 = [{dog: true}, {cat: false}, {mouse: false}]

I can't find a way how to create keys from these string?

Upvotes: 0

Views: 41

Answers (1)

Ori Drori
Ori Drori

Reputation: 191976

Map the first array, and use computed property names to generate the keys from the current array item. Generate the value by checking if the 2nd array includes the current key.

const arr1 = ['dog', 'cat', 'mouse']
const arr2 = ['dog']

const result = arr1.map(key => ({
  [key]: arr2.includes(key)
}))

console.log(result)

Upvotes: 2

Related Questions