How to manipulate an array, eliminating the empty items? [duplicate] - javascript

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

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 fill multidimensional array with empty arrays [duplicate]

This question already has answers here:
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 25 days ago.
I am trying to initialize a two-dimensional array with empty arrays so I can add elements to them in a larger composition using Array.push. However, when I add to the inner arrays, they all get added to. Here is a simple example:
const arr = Array(3).fill([]);
arr[0].push(42);
Now arr is [[42],[42],[42]] but I was hoping for [[42],[],[]].
I think the problem is Array.fill is putting the same referenced empty array into each slot. How do I get fill to make a distinct empty array at each slot?
You can use Array#map.
const arr = [...Array(3)].map(_ => []);
arr[0].push(42);
console.log(arr);
Or Array.from.
const arr = Array.from({length: 3}, _ => []);
arr[0].push(42);
console.log(arr);

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
}

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

Make a new array with one element of a multidimenional array using JavaScript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have a JSON object in a JavaScript function:
[{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}]
That is saved to an Multi-dimensional Array
Is there a better way that I can make a make a new Array with only the PMID element without looping and building it. I know that this question is close to a duplicate but I'm not interested in a merge or a concat. Currently I'm doing
var newArray = [];
for (var i = 0; i < this.pmidList.length; i++) {
newArray.push(this.pmidList[i]["PMID"]);
}
This question maybe a duplicate but if the average person can't find it based upon the title then it should not be considered a duplicate. The solution is the same but the question titles are much different.
You can use the Array.map function.
const input = [{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}];
const output = input.map(item => item.PMID);
Documentation

Categories

Resources