A
A
Alex102142021-06-11 18:31:57
JavaScript
Alex10214, 2021-06-11 18:31:57

How to put object values ​​into an array?

Hello, I have an array like this:

const arr = [
  {
    value: 'aaa'
  },
  {
    value: 'bbb'
  },
  {
    value: 'ccc'
  },
  {
    value: 'ddd'
  },
]

From it I want to get this:
const arr2 = ['aaa', 'bbb', 'ccc', 'ddd'];
This is what I do:
for (let el of arr) {
  this.arr2.push(el.value);
}

I am getting this error:
ERROR TypeError: Cannot read property 'push' of undefined
    at SafeSubscriber._next (main-graph.component.ts:87)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
    at SafeSubscriber.next (Subscriber.js:122)
    at Subscriber._next (Subscriber.js:72)
    at Subscriber.next (Subscriber.js:49)
    at TakeSubscriber._next (take.js:35)
    at TakeSubscriber.next (Subscriber.js:49)
    at TakeSubscriber._next (take.js:35)
    at TakeSubscriber.next (Subscriber.js:49)
    at MapSubscriber._next (map.js:35)

Can you tell me what is happening and how to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Steppp, 2021-06-11
@Alex10214

const arr = [
  {
    value: 'aaa'
  },
  {
    value: 'bbb'
  },
  {
    value: 'ccc'
  },
  {
    value: 'ddd'
  },
]

let arr2 = [];

arr.forEach(el => {
   arr2.push(el.value)
})

console.log(arr2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question