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

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]

Related

javascript how to filter various items in array [duplicate]

This question already has answers here:
remove objects from array by object property
(15 answers)
Filter array of objects based on another array in javascript
(10 answers)
How to remove objects from an array which match another array of ids
(4 answers)
Closed 10 months ago.
I need a function that filter some items from array ,like below:
how to make it? Thank you so much in advance !
const users = [
{id:"1",name:"Jane"},
{id:"2",name:"Lucy"},
{id:"3",name:"Li Li"},
{id:"4",name:"Hilton"},
{id:"5",name:"Rose"},
{id:"6",name:"Ha Ha"},
]
//user with this id need to been remove.
const filteredUsers = [
'1','2','5'
]
function filterUsers(allUsers,filteredUsers){
//return after filtered list
}

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);

Selecting last item in array javascript [duplicate]

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];

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

Categories

Resources