8

How can I made new array from firsts elements of arrays from this array ?

[["1",2],["3",2],["6",2]] 

and I want it to be

['1', '3', '6'] 

My attempt:

var newArray = []

for (i = 0; i < arrayToCompare.length - 1; i++) {
    newArray.push(arrayToCompare[[0]])
}

5 Answers 5

8

You could just use a simple map and destructure the first element:

const arr = [["1", 2],["3", 2],["6", 2]]
console.log(arr.map(([e]) => e))

The ([e]) part of that before the => is destructuring the parameter using array destructuring. It means that for each subarray passed to the map callback, e receives the value of the first element of the subarray. In ES5 and earlier, the ([e]) => e arrow function would be function(entry) { return entry[0]; }

Still, if you still don't understand the concept, prefer efficiency, or just want to go back to basics, you could use the trusty for loop, making sure to push only the first element of each subarray:

const arr = [["1", 2],["3", 2],["6", 2]]

const output = []
for (let i = 0; i < arr.length; i++) {
  output.push(arr[i][0])
}

console.log(output)

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

3 Comments

Would you care to comment on the construct ([e])
@mplungjan - Yeah, I thought that was missing, too, so I added it. :-) Kobe, hope you don't mind.
@T.J.Crowder Thanks for that, I was just about to go into detail but you handled it nicely for me :)
2

Try this:

let arr = [["1", 2], ["3", 2], ["6", 2]];
let res = arr.map(val => {
    return val[0]
})
console.log(res);

8 Comments

arr.map(val => val[0])
@mplungjan edited :). Btw just want to know what was the issue with that?
@SaurabhAgrawal - I think mplungjan was pointing out that you can use a concise arrow function there instead of a verbose one: .map(val => val[0]). (Yes, the () you edited out are optional as well, but I don't think that was the main point.)
@SaurabhAgrawal - No, they do not. They're purely part of the syntax of an arrow function parameter list. They're optional when there's exactly one parameter. It makes no different whatsoever to the execution of the function.
@SaurabhAgrawal - I suppose, he meant Extra () was not needed as you were using only single argument :)
|
2

You can use Array.prototype.map() to crate a new array with the item from the first index:

var arr = [["1",2],["3",2],["6",2]]
var newArray = arr.map(i => i[0]);
console.log(newArray);

Comments

2

This one also works

console.log(Object.keys(Object.fromEntries([["1", 2],["3", 2],["6", 2]])))

In this example, Object.fromEntries will create an object from an array of key/value pairs - it will take the first element as a key, and the second element as the value - creating something like this:

{
  "1": 2,
  "3": 2,
  "6": 2
}

Then, Object.values will grab the keys of the object, thus, removing the values and retaining the keys, giving the desired output.

P/S: just added another way to do this

console.log(Array.from([["1", 2],["3", 2],["6", 2]], x=>x[0]))

1 Comment

LOL. Inventive!
0

Use map and get the first element using shift method. PS: not very efficient because of ...(spread) operator for each element.

const arr = [["1",2],["3",2],["6",2]];

const arrFirsts = arr.map(items => [...items].shift());

console.log(arrFirsts)
console.log(arr)

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.