1

I have a array of arrays:

[["email", "[email protected]"], ["phone", 123123123], ["address", "street"]]

I want to create array where I will have only first element from every array:

["email", "phone", "address"]

I have something like that:

bigArray.map((field) => {
        this.smallArray.add(field[0]);
      });

but I got error: Error in event handler for "bigArray": "TypeError: _this.smallArray.add is not a function"

How can I solve this issue?

3 Answers 3

1

const arr = [["email", "[email protected]"], ["phone", 123123123], ["address", "street"], ["sameple"]]
const result = arr
  .filter(a => !!a[1])
  .map(a => a[0])

console.log(result)

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

3 Comments

one more thing: How can I add condition that this value should be added only when item[1] is empty?
Updated answer.. First you should filter
Perfect, Thank you!
1
this.smallArray = this.bigArray.map(arr => arr[0])

1 Comment

one more thing: How can I add condition that this value should be added only when item[1] is empty?
0

I think you are tyring to add filtered element to smallArray. You should use.

this.smallArray.push(field[0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.