Remove array elements matching element data [duplicate] - javascript

This question already has answers here:
remove objects from array by object property
(15 answers)
Closed 2 years ago.
I have
stone = [];
stoneB.playerId = players[player.playerId].playerId
I push new elements into stone by
stone.push(stoneB)
How would I go about removing all of the elements from stone that match stoneB.playerId for a given player?

.filter is probably your best bet here:
stone = stone.filter(d => d.playerId !== stoneB.playerId)
or more generally:
function remove(stone, playerId) {
return stone.filter(d => d.playerId !== playerId);
}
stone = remove(stone, stoneB.playerId);

Related

filter an HTML-elements filled array (javascript) [duplicate]

This question already has answers here:
How to filter an array from all elements of another array
(24 answers)
Closed 2 years ago.
i have two arrays that both contains HTML Elements, i want to remove from the first array all the elements that are also present in the second one.
var a = [document.createElement('div'), document.createElement('span')],
b = [document.createElement('span'), document.createElement('p')];
filter(a,b) == [document.createElement('span')] // true
i tried with something like this, but it seems like it doesn't work:
var filter = function(a,b) {
return a.filter(element => {
return b.map(element_ => {
return element.isEqualNode(element_);
}).includes(false);
});
}
let res = a.filter((el)=> { return b.find((el2)=> el.localName === el2.localName)});
// res = [span];
So, el.localName is a property of a HTML element of type string which contains the name of the element. Which can be used for comparing two HTML elements
For Example: In Case of div its 'div'
Code returns the common element present in both the arrays.

Is there .count method in javascript like python? [duplicate]

This question already has answers here:
Idiomatically find the number of occurrences a given value has in an array
(11 answers)
How to count certain elements in array?
(27 answers)
Closed 2 years ago.
In Python, if we have a list,
array = [1,2,3,4,5]
If we say array.count(1), it will return the count of 1 in array.
Is there a method like this in javascript?
I think there isn't. But you can make one by using prototype.
let array = [1,2,3,4,5,1];
Array.prototype.count = function(value) {
let count = 0;
this.forEach(item => {
if (item === value) {
count++;
}
});
return count;
}
console.log(array.count(1));
console.log(array.count(2));

Check if object property value is unique and if not filter out - JS [duplicate]

This question already has answers here:
Create array of unique objects by property
(17 answers)
Closed 4 years ago.
I am building a small webapp and I am using this API which has around 485 objects in it. Those objects have specific property center. It can happen that the center property sometimes is equel (and not unique) to other object' center. How can I filter out the duplicates?
My code so far
function call() {
fetch('https://data.nasa.gov/resource/9g7e-7hzz.json')
.then((response) => {
return response.json();
})
.then((data) => {
locations = data;
getLocations(locations);
})
}
function getLocations(locations) {
//Filter out duplicates and push it to an array
locations.forEach((location) => {
console.log(location.center);
})
}
So the result I expect is an array with objects, with unique values in the center property. Can someone help me out?
Set can help you here:
function getLocations(locations) {
let centers = new Set();
let uniques = [];
for (let location of locations) {
if (!centers.has(location.center)) {
centers.add(location.center);
uniques.push(location);
}
}
return uniques;
}

JavaScript Array undefined element [duplicate]

This question already has answers here:
How can I add new array elements at the beginning of an array in JavaScript?
(12 answers)
Closed 6 years ago.
I´ve got a function which should add an element at the start of an array.
But I always get an undefined element at the end of my array. I hope someone can help me :)
function putToFirst(e){
var array = [];
array.push(e);
this.arrayList = array.concat(this.arrayList);
}
EDIT:
class List {
constructor () {
super()
this.arrayList = [];
}
putToFirst(e) {
this.ArrayList.unshift(e);
}
}
thats the class. I create a new object from the class list and call the function putToFirst on this object. But I always get an Array with 'undefinded' in the end
this.arraylist is wrong this represent a current object you are not currently using any class in above code you just need to change
function putToFirst(e){
var array = [];
var arrayList=[];
array.push(e);
arrayList = array.concat(arrayList);
console.log(arrayList);
}

How to remove duplicated objects from an Array in javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I am using above code and removing the items based on id
I am getting id is not defined while comparing i+1 item when i reaches to maximum array length.
Appreciate your help....
var arr = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":474.5,"top":16},{"id":"0-block-2","left":686.5,"top":0}];
Expecting outout = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":686.5,"top":0}]
Array.prototype.unique = function(){
var passNum = this.length;
var _self = this;
// to repeat loops n*n times
while(passNum>0){
for(var i = 0;i<this.length;i++) {
if(this[i].id==this[i+1].id){
var _indx = this.indexOf(this[i]);
this.splice(_indx,1);
}
}
passNum = passNum-1;
}
return this;
};
You can do this with ES2015 Sets.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
EDIT: If you can't use ES2015, there is a polyfill available.

Categories

Resources