Remove object from array? [duplicate] - javascript

This question already has answers here:
Remove Object from Array using JavaScript
(32 answers)
Remove an item from array using UnderscoreJS
(12 answers)
Closed 9 years ago.
I currently have a function that successfully removes an item from an array but only for a simple collection. I am trying to extend this so it also works for removing object from an array but stumped. Can anyone help? Here is what I got so far:
remove: function (arr, value) {
if (_.isObject(value)) {
//HOW TO HANDLE THIS, MAYBE USE THIS FOR HELP? _.where(arr, value)
} else {
//THIS WORKS!
if ($.inArray(value, arr) >= 0)
arr.splice($.inArray(value, arr), 1);
}
}
For the first if-statement, I am having trouble finding the index of the object. Using underscore.js I can find the object itself, but don;t know how to find the index so I can remove it from there. Any ideas to solve this or a better way?

Related

How to use filter to compare to arrays to find missing object? [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.

Why do this returns a non expected result? [duplicate]

This question already has answers here:
Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
(14 answers)
Closed 4 years ago.
So I'm learning about the reduce method and I write this code...
function getSum(x,y){
return x+y
}
var arraySum = function(array) {
return array.reduce(getSum)
};
arraySum([1,[2,3],[[4]],5])
But it actually returns a string of the elements all-together...
I'm actually trying to sum them all... I expected the result to be 15 instead of "12,345"
What's happening? what am I doing wrong?
Your problem is you're adding actual arrays together, not the content within the arrays. Add a counter, and an incrementational variable like i inside the variable arraySum, and call the position of i, incrementing it every time, it should fix your problem.

Javascript object traversal in wrong way [duplicate]

This question already has answers here:
How to keep an Javascript object/array ordered while also maintaining key lookups?
(2 answers)
Closed 7 years ago.
I have a json object in occupancy_list as follows :
I am traversing the object as follows :
for(var prop in occupancy_list)
{
console.log(prop);
}
I am getting values in reverse order. Like at first Room 2 then Room 1. How can I fix it ?
Object properties are unsorted and could always be random. If you need order - get the keys, and iterate them that way:
Object.keys(occupancy_list).sort(function(a, b) {
//sort logic
}).forEach(key) {
//logic on each key
});

Finding specific objects in array to remove [duplicate]

This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
Closed 7 years ago.
I have an array of data which stores an object with functions and other such info. I push these objects to the function for my draw function to execute.
But i do not know of a way to find a specific object in an array to remove it and thus stop drawing it.
For example i have an array structure like this:
var data = {
'fnc':function(){
updatePosition(spriteID);
drawSprite(spriteID);
},
'something':'here'
};
var drawOrder= [];
drawOrder.push(data);
There are many functions in this array and they are pushed dynamically depending on what i wish to draw.
What would be the best way to find the index of one of these objects and remove it from the array in this case?
indexOf() returns the index in the array of the element you're searching for, or -1. So you can do:
var index = drawOrder.indexOf("aKey");
if (index != -1)
drawOrder.splice(index, 1);
Splice:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
indexOf:
http://www.w3schools.com/jsref/jsref_indexof_array.asp
I'm not 100% this will answer your question cause is not clear at least to me.
If you want to remove the whole element but you are worried about founding the right index before actually splice the array you should use Array.indexOF
See this code below:
var data = {
'fnc':function(){
updatePosition(spriteID);
drawSprite(spriteID);
},
'type':'aabb'
};
var drawOrder= [];
drawOrder.push(data);
console.log(drawOrder);
drawOrder.splice(drawOrder.indexOf(data), 1);
console.log(drawOrder);
As the documentation reports:
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

How to completely delete element from Javascript array [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 7 years ago.
I am using this code to iterate over an array and delete the element if it contains no itemsToPost.
rssFeeds.forEach(function(feed, index, array){
if(feed.itemsToPost.length == 0){
delete rssFeeds[index]
}
});
However, the array that I receive from this, assuming that all elements are deleted looks like:
[ , ]
And when I run this against Lodash's method to test if an array is empty.
console.log(_.isEmpty(array))) // returns 'false'
How can I completely delete these elements such that the array is truly empty?
Update:
I've found a solution using Array.prototype.filter as was suggested, such as:
function hasNewItems(array) {
return array.itemsToPost.length > 0;
}
var newArray = rssFeeds.filter(hasNewItems)
console.log(newArray); // returns []
It isn't necessarily deleting the array but it has the desired effect.
delete is not the correct way to remove element from array, you should use rssFeeds.splice(index, 1) instead.

Categories