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

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

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 manipulate an array, eliminating the empty items? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);

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

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

Categories

Resources