3

for example i have this array

    var array1 = [{ uid=24433357, first_name="fname", last_name="lname", ...},
{ uid=4821888, first_name="fname", last_name="lname", ...},
{ uid=677614, first_name="fname", last_name="lname", ...},
{ uid=4789723, first_name="fname", last_name="lname", ...},
{ uid=444464, first_name="fname", last_name="lname", ...},
{ uid=767687867, first_name="fname", last_name="lname", ...}]

i want to remove element from array1 where uid = x

i have no idea how?

for normal arrays i know this method

array1.splice(index, count)
2
  • 1
    Loop through all elements in array1. If uid of array1[i] = x add it to array2. Once your loop's finished array1 = array2. Commented Jan 27, 2014 at 10:24
  • Your array will have many syntax errors in it btw. Commented Jan 27, 2014 at 10:29

2 Answers 2

5

Fix your objects first, the syntax is all wrong. See below and then use the Array#filter function to filter out the unwanted object by its uid

var array1 = [{ uid:24433357, first_name:"fname", last_name:"lname"},
    { uid:4821888, first_name:"fname", last_name:"lname"},
    { uid:677614, first_name:"fname", last_name:"lname"},
    { uid:4789723, first_name:"fname", last_name:"lname"},
    { uid:444464, first_name:"fname", last_name:"lname"},
    { uid:767687867, first_name:"fname", last_name:"lname"}];

ES5 not suitable for old browsers

array1 = array1.filter(function(o){
    return o.uid !== 24433357
});

Classic Way compatible with old browsers

for (var i = 0; i < array1.length; i++) {

    if (array1[i].uid === 24433357) {
        array1.splice(i, 1);
    }
}

console.log(array1);
Sign up to request clarification or add additional context in comments.

1 Comment

I know that the syntax is incorrect, the syntax is not important in this case, the main thing here is solution ))) thanks)))
2
for(var i = array1.length-1; i--;){
    if (array1.uid[i] === "x") array1.splice(i, 1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.