Selecting last item in array javascript [duplicate] - javascript

This question already has answers here:
Selecting last element in JavaScript array [duplicate]
(13 answers)
Closed 6 years ago.
So say I have an array and the user can add do it by a input html tag:
var example=["a","b"];
var command=document.getElementByID("id");
so idk how long the array will be when i execute the next step,which is selecting the last item in the array and register it in an object
example.split(",")
someObject[//how do i chose the last item?]

You can select the last item in a javascript array like this
arr = example.split(",");
lastArr = arr[arr.length - 1];

Related

Filter Out Element from Nested Array [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
How to remove false values from array?
(6 answers)
Closed 3 days ago.
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];
I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated :)
Directly use Array#filter:
let input=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);

How to get the one before last element of an array in JavaScript? [duplicate]

This question already has answers here:
get the second to last item of an array?
(11 answers)
Closed 10 months ago.
I wanna to get one before last element of array in JavaScript. but in console, when I use this code "array.length -2"dose not work, and it just return me the length of array minus 2.
and when I use this code "array.at[-2]" it returns "undefined"!
const arr = [1,2,3,4,5,6]
console.log(arr[arr.length -2])
//5

Javascript how to get non duplicates data from array? [duplicate]

This question already has answers here:
Finding items that appear only one time in a Javascript array
(5 answers)
Completely removing duplicate items from an array
(11 answers)
Closed 11 months ago.
let getNonDuplicates = [1,2,3,4,2,3,4,5]; // Here's my example array
the result I needed is to get 1 & 5 since those are the data that don't have duplicates
let resultMustBe = [1,5];
You could use filter method, to filter the array, based on the condition that index of an element from beginning is equal to index of the same element from the last. It means that the element is unique in the array.
let arr = [1,2,3,4,2,3,4,5];
let res = arr.filter(e=>arr.indexOf(e)===arr.lastIndexOf(e));
console.log(res);

Push data into a certain position in an array [duplicate]

This question already has answers here:
How to push to an array in a particular position?
(4 answers)
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
How do I push some data into a certain place in an array?
I am trying the code below:
Keys[1].push({ Keys: "Test" });
But it doesn't seem to work as expected
The method you are looking for is splice
arrayObject.splice(index,0,{Keys:"Test"});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

select last array without knowing the total number of item [duplicate]

This question already has answers here:
Get the last item in an array
(59 answers)
Closed 8 years ago.
object = [
{"id":1,"name":"Joe"}
{"id":2,"name":"Joe1"}
{"id":3,"name":"Joe2"}
{"id":4,"name":"Joe3"}
]
I want to select {"id":4,"name":"Joe3} but I can't do like object[3] so it there any way to retrieve the last object of an array??
You can get the total length of an array by using length.
array[array.length-1]

Categories

Resources