how to remove object from array using javascript or angular js? - javascript

I am trying to remove object from array and get the resulted array.I am using remove function but it is not working perfectly
Here is my input
I need to remove all value of array which have "file": "Id"
here is my fiddle
for (var i = 0; i < arr.length; i++) {
for (var k = 0; i < arr[i].columns.length; i++)
arr[i].columns[0].remove()
}

Slight error in incrementing k
for(var i=0; i<arr.length;i++){
for (var k=0; k<arr[i].columns.length;k++)
delete arr[i].columns[0]
}
Updated JS Fiddle

Can you try this function to remove all value of array which have "file": "Id"
for(var i=0; i<arr.length;i++){
for (var k=0; k < arr[i].columns.length; k++){
if(arr[i].columns[k].fieldNameOrPath == 'Id'){
arr[i].columns.splice(k,1)
}
}
}

you can use Underscore.js
var result = _.map(arr, function(obj){
return {
columns: _.reject(obj['columns'], function(innerObj){
return innerObj['fieldNameOrPath'] == 'Id';
})
}
});

Related

How to push data to array with position [i]?

For example i've created an array in a for loop:
for (i = 0; i < data.length; i++){
array[i] = {
label: bread,
color: colorArray[i]
};
}
In a child for loop i'd like to append more data to this array. What I tried:
for (r = 0; r < data[i].length; r++){
array[i].push({
data: breadTypes[r][i]
});
}
Which throws TypeError : array[i].push is not a function.
array[r] = { data: breadTypes[r][i] }; overwrites the existing data as expected.
Is there a different way to do this? Thanks!
Here array[i] is an object and push is an array method which cannot be used on an object but you can create data key in array[i] object
array[r].data = breadTypes[r];
If the second loop is not nested inside the first one then breadTypes[r][i] will throw an error since i will be be available
Just do like this:
for (i = 0; i < data.length; ++i) {
array[i].data = breadTypes[i];
}
If i understand correctly, your second for should look like this:
for (r = 0; r < data[i].length; r++){
array[i].data = breadTypes[r][i];
}

JavaScript iterate through an object arrays(unknown number of content array)

I am trying to iterate through an object which has unknown number of arrays and also i dont know number of data in each array because im pulling it from a database like this for example.
i can know how many arrays in object with data.length so first for loops condition will be data.length but because i dont know how many data in each array how can i decide second for loop condition ? thanks
for(var i = 0; i <= data.length; i++) {
for(var k = 0; k <= ????; k++) {
data[i][k].locationlat
}
}
So you want to iterate through a 2-dimensional array? Would something like this work?
var num_d1 = data.length;
for (var i=0; i<num_d1; i++) {
var num_d2 = data[i].length;
for (var k=0; k<num_d2; k++) {
console.log('Latitude: ', data[i][k].locationlat)
}
}

accessing all of my elements in a multi-dimensional array with multiple loops. Why is this coming out as undefined?

Here's what I have, I have a 3-dimensional array with variations of my name. The whole thing looks like this.
var array = [['kenny', 'Kenney'],['ken','Ken'],['kenneth', 'Kenneth']];
Now, there's nothing wrong with it from what I can tell but then comes what I would like to do. I'm just trying to utilize a for loop to just loop through all of this at once so all of these elements show up. Here's what I wrote down for myself..
for(var i = 0; i < array.length; i++) {
for(var j = 0; j < array.length[i]; j++) {
for(var k = 0; k < array.length[i][j]; k++) {
console.log(array[i][j][k]);
}
}
};
What comes back is undefined. What exactly am I doing wrong? Thanks in advance, guys.
You have 2 issues here. First, you are trying to go 3 "levels" deep as if this was a 3-dimensional array, but it is only 2-dimensional.
You also need to do array[i].length instead of array.length[i]
var array = [
['kenny', 'Kenney'],
['ken', 'Ken'],
['kenneth', 'Kenneth']
];
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
console.log(array[i][j]);
}
};
You put the indexes in the wrong spot:
for(var i = 0; i < array.length; i++) {
for(var j = 0; j < array[i].length; j++) {
for(var k = 0; k < array[i][j].length; k++) {
console.log(array[i][j][k]);
}
}
}
The last loops is not needed, because that will go through the words in each of your items, printing word for word each element.
Each element of the first array has to be treated as an array.
var array = [['kenny', 'Kenney'],['ken','Ken'],['kenneth', 'Kenneth']];
for (var i=0; i<array.length; i++) {
for (var j=0; j < array[i].length; j++) {
console.log(array[i][j]);
}
}
Using forEach is clearer:
var array = [['kenny', 'Kenney'],['ken','Ken'],['kenneth', 'Kenneth']];
array.forEach(function(subarray) {
subarray.forEach(function(name) {
console.log(name);
});
});

Iterate over a JSON Array in JavaScript or jQuery

I need to loop over a JSON Array which comes from the server. It looks exactly like this:
[{"username":"betontester"},{"username":"kuesst"},{"username":"master_pat"},{"username":"olli"},{"username":"test15"},{"username":"test20140216"},{"username":"test789"},{"username":"tester100"},{"username":"tobi"}]
I found some advices here, but none of them seem to work for me.. Is there any easy way to get all these values?
var arr = [{"username":"betontester"},{"username":"kuesst"},{"username":"master_pat"},{"username":"olli"},{"username":"test15"},{"username":"test20140216"},{"username":"test789"},{"username":"tester100"},{"username":"tobi"}];
for(var i = 0; l = arr.length; i< l; i+=1)
{
console.log(arr[i].username); // log username
}
another option:
arr.forEach(function(i) {
console.log(i.username)
});
var data = [{"username":"betontester"},{"username":"kuesst"},{"username":"master_pat"},{"username":"olli"},{"username":"test15"},{"username":"test20140216"},{"username":"test789"},{"username":"tester100"},{"username":"tobi"}],
usernames = [];
for(var i=0; i<data.length; i++) {
usernames.push(data[i].username);
}
try this:
var o = [{"username":"betontester"},{"username":"kuesst"},{"username":"master_pat"},{"username":"olli"},{"username":"test15"},{"username":"test20140216"},{"username":"test789"},{"username":"tester100"},{"username":"tobi"}];
for(var i = 0; i < o.length ; i++)
for(var j in o[i])
alert(j + ": " + o[i][j]);
If you don't need IE<9 compatibility, you can use Array.prototype.map()
var arr = [{"username":"betontester"},{"username":"kuesst"},{"username":"master_pat"},{"username":"olli"},{"username":"test15"},{"username":"test20140216"},{"username":"test789"},{"username":"tester100"},{"username":"tobi"}],
usernames = arr.map(function(item) {
return item.username;
});

Compare two arrays in javascript

I'd like to check which elements are equal in my two arrays, but can't get it working.
This is my code:
for (var i; i < bombs.length; i++) {
for (var j; j < bombsDb.length; j++) {
if (bombs[i].name === bombsDb[j].address) {
console.log(bombs[i].name);
} else {
console.log("non-equal elements");
}
}
}
So the first array contains objects from the google places api and the second one contains data from my database.
Thanks in advance!
You have to initialize i and j;
for (var i = 0; i < bombs.length; i++) {
for (var j = 0; j < bombsDb.length; j++) {
if (bombs[i].name === bombsDb[j].address) {
console.log(bombs[i].name);
} else {
console.log("non-equal elements");
}
}
}
Comparing can also be done using the .not selector from jquery. Check this:
var a = [1,2,3,4,5,6];
var b = [4,5,6,7,8,9];
$(a).not( $(a).not(b).get() ).get();
This will return the following array
[4,5,6]
You are missing the initial assignment to i and j in your for loop.
// here
// v
for (var i = 0; i < bombs.length; i++) {
// your loop
}
This causes the comparision to return false in the first iteration of the loop since undefined < bombs.length always return false, so it will not proceed.

Categories