stepheniok
stepheniok

Reputation: 405

JavaScript - Pushing an index of an array

I am attempting to push one index of my array into it's own array. I am attempting this by using the forEach() method.

I have an array that is nested inside of an array revData that has multiple indexes inside of its array. I expecting to push out only index 5 of the array into its own array so I can graph the data.

At the moment using doing my forEach method, my newArr only has the first index 5 five times.

My expected out come is to have the newArr have 3 results coming from the values of index 5 from revData such as :

newArr = [ 24343.1 , 44321.0, 43242.8 ]

Here is an example of my code :

let revData = [ 
[1, 1, 1, "22", "online stores", 24343.1  ], 
[2, 2 ,2, "13", "retail stores", 44321.0], 
[ 3, 3, 3, "7", "walk ins", 43242.8]
] 

const newArr = []


 revData[0].forEach(function () {
        newArr.push(revData[0][5])
    })

console.log(newArr)

Upvotes: 0

Views: 46

Answers (4)

Daniel Choi
Daniel Choi

Reputation: 19

The language here is a little off and I'll do my best to answer.

When you say "push out" I am assuming you want to remove the element out of the array. The correct term for removing an element from the array is "pop". "Pop" is generally related to removing an element an array, while "push" is generally related to adding an element to an array.

Also Do not name your newArr as a const. const is meant to indicate values that never change. By naming your newArr as a const, it cannot change.

Now to answer your question, I can see what you are trying to do, but your syntax is a little wrong.

let revData = [ 
  [1, 1, 1, "22", "online stores", 24343.1  ], 
  [2, 2 ,2, "13", "retail stores", 44321.0], 
  [ 3, 3, 3, "7", "walk ins", 43242.8]
]
let newArr = []


 revData.forEach(function(item) {
        newArr.push(item[5])
  })

console.log(newArr)

https://codepen.io/junghole/pen/BajdZPq?editors=0012

I suggest that you gain a better understanding of 2d arrays and for each loops. A good resource is geeks for geeks, or simply googling. Good Luck!

Upvotes: 0

Rounin
Rounin

Reputation: 29453

One available approach here is to use a for... of loop.

Everyone will have their own preferred approach, but I generally find that, for syntactic clarity, I favour for...of loops over forEach.

Working Example:

let revData = [ 
  [1, 1, 1, "22", "online stores", 24343.1  ], 
  [2, 2 ,2, "13", "retail stores", 44321.0], 
  [3, 3, 3, "7", "walk ins", 43242.8]
]; 

const newArr = [];

for (arrayElement of revData) {

  newArr.push(arrayElement[5]);
}

console.log(newArr);

Upvotes: 0

0bero
0bero

Reputation: 1062

You are iterating over revData[0] so you won't get the other items. Also, during the iteration you always read the value in revData[0]. You should use the first parameter of the forEach callback, like this:

let revData = [
  [1, 1, 1, "22", "online stores", 24343.1],
  [2, 2, 2, "13", "retail stores", 44321.0],
  [3, 3, 3, "7", "walk ins", 43242.8]
]

const newArr = []


revData.forEach(function(item) {
  newArr.push(item[5])
})

console.log(newArr)

Upvotes: 2

halilcakar
halilcakar

Reputation: 1648

Probably it should be like this

let revData = [
  [1, 1, 1, "22", "online stores", 24343.1],
  [2, 2, 2, "13", "retail stores", 44321.0],
  [3, 3, 3, "7", "walk ins", 43242.8],
];
const newArr = [];
revData.forEach(function (child) {
  newArr.push(child[5]);
});
console.log(newArr);

Or you can use map instead

const newArr = revData.map((child) => child[5]);
console.log(newArr);

Upvotes: 3

Related Questions