0
let array=[
  [
    "Test1",
    "4",
    "160496"
  ],
  [
    "Test2",
    "6",
    "38355"
  ],
  [
    "Test3",
    "1",
    "1221781"
  ],
  [
    "Test4",
    "3",
    "124315"
  ],
  [
    "Test5",
    "5",
    "29509"
  ],
  [
    "Test6",
    "7",
    "47742"
  ],
  [
    "Test7",
    "2",
    "231034"
  ]
]

I need only the first two indexes from an array of array Ex:

[["Test1","4"],["Test2","6"],["Test3","1"],["Test4","3"],["Test5","5"],["Test6","7"],["Test7","2"]]
Nick
  • 138,499
  • 22
  • 57
  • 95
Sankar
  • 23
  • 6

4 Answers4

0

Tray using array.map to generate new array from an existing one

const array = [
  ["Test1", "4", "160496"],
  ["Test2", "6", "38355"],
  ["Test3", "1", "1221781"],
  ["Test4", "3", "124315"],
  ["Test5", "5", "29509"],
  ["Test6", "7", "47742"],
  ["Test7", "2", "231034"]
];
const newArray = array.map(item => [item[0], item[1]]);
console.log(newArray);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
0

I am assuming that you don't want to alter the original array variable and you want to remove only the last index, no matter how long the inner array is, the below solution may help.

let array=[
  [
    "Test1",
    "4",
    "160496"
  ],
  [
    "Test2",
    "6",
    "38355"
  ],
  [
    "Test3",
    "1",
    "1221781"
  ],
  [
    "Test4",
    "3",
    "124315"
  ],
  [
    "Test5",
    "5",
    "29509"
  ],
  [
    "Test6",
    "7",
    "47742"
  ],
  [
    "Test7",
    "2",
    "231034"
  ]
]

const newArray = array.map(innerArray => innerArray.slice(0, -1));

console.log(newArray);
Kevin S
  • 1
  • 1
  • 1
0

This wil iterate through your array and remove the last element of each nested array

for (let nestedArray of array) {
    let lastIndex = nestedArray.length - 1
    nestedArray.splice(lastIndex, 1)
}

Read about splice here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

birgersp
  • 3,909
  • 8
  • 39
  • 79
0

let array=[
  [
    "Test1",
    "4",
    "160496"
  ],
  [
    "Test2",
    "6",
    "38355"
  ],
  [
    "Test3",
    "1",
    "1221781"
  ],
  [
    "Test4",
    "3",
    "124315"
  ],
  [
    "Test5",
    "5",
    "29509"
  ],
  [
    "Test6",
    "7",
    "47742"
  ],
  [
    "Test7",
    "2",
    "231034"
  ]
];

array.forEach(x=>
  (x.splice((x.length-1), 1))?x:false
);
 console.log(array);
  • `array.map` anyways create a new array, then why are we disturbing the original array? also why this answer was downvoted without any comment? any reason for that? – Nitheesh Feb 12 '21 at 06:49
  • @Nitheesh You probably answered your last two questions with your first one. – VLAZ Feb 12 '21 at 06:59
  • @VLAZ sorry? could not catch that – Nitheesh Feb 12 '21 at 08:10
  • @Nitheesh your last two questions were "*why this answer was downvoted without any comment? any reason for that?*" and I think the answer is your first question: "*array.map anyways create a new array, then why are we disturbing the original array?*" – VLAZ Feb 12 '21 at 09:40