javascript - splice row from array by value - Stack Overflow

I want splice the line with value = 3[3,"John", 90909090]data.json{"headers":[[{&

I want splice the line with value = 3

[3,"John", 90909090]

data.json

{
"headers":[[
{"text":"Code","class":"Code"},
{"text":"Code","class":"Code"}
]],
"rows":[
[0,"Peter", 51123123],
[3,"John", 90909090],
[5,"Mary",51123123]
],
"config":[[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
"other":[[13,0]]
}

I try this:

var size = data.rows.length; // number of rows

var del = 3 // Value of ID to be deleted          

for (i = 0; i < size; i++) {  

var id = data.rows[i][0];                  

    if(del==id){  // if del = id -> splice                                         

       data.rows.splice(i,1);

    }

}

Results:

Only splice or only loop this code works.

But, with both show this error:

Uncaught TypeError: Cannot read property '0' of undefined(…)

It occurs in "data.rows[i][0]"

I want splice the line with value = 3

[3,"John", 90909090]

data.json

{
"headers":[[
{"text":"Code","class":"Code"},
{"text":"Code","class":"Code"}
]],
"rows":[
[0,"Peter", 51123123],
[3,"John", 90909090],
[5,"Mary",51123123]
],
"config":[[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
"other":[[13,0]]
}

I try this:

var size = data.rows.length; // number of rows

var del = 3 // Value of ID to be deleted          

for (i = 0; i < size; i++) {  

var id = data.rows[i][0];                  

    if(del==id){  // if del = id -> splice                                         

       data.rows.splice(i,1);

    }

}

Results:

Only splice or only loop this code works.

But, with both show this error:

Uncaught TypeError: Cannot read property '0' of undefined(…)

It occurs in "data.rows[i][0]"

Share Improve this question asked Nov 16, 2016 at 12:03 GurigraphicsGurigraphics 3313 silver badges12 bronze badges 2
  • 1 This is a good example of premature micro-optimization wasting your time. There isn't any need to use that size variable; just pare with data.rows.length directly. If you had, you wouldn't have run into this error. (You also would have kept processing further entries. Whether you want to do that depends on whether 3 could appear more than once in the array...) – T.J. Crowder Commented Nov 16, 2016 at 12:06
  • Truth. \o/ Thank you. – Gurigraphics Commented Nov 16, 2016 at 12:15
Add a ment  | 

4 Answers 4

Reset to default 3

instead of using a for loop, id use the array filter function:

data.rows = data.rows.filter(function(row){
    return row[0] !== del;
});

Just add a break to the condition, because the next element, is the one you have spliced, which is not anymore in the array.

if (del == id) {  // if del = id -> splice
   data.rows.splice(i, 1);
   break; // no more to search
}

You can iterate with Array#forEach():

var data = {"headers": [[{"text": "Code","class": "Code"}, {"text": "Code","class": "Code"}]],"rows": [[0, "Peter", 51123123],[3, "John", 90909090],[5, "Mary", 51123123]],"config": [[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],"other": [[13, 0]]},
    del = 3; // Value of ID to be deleted

data.rows.forEach(function(item, index) {
  item[0] === del && data.rows.splice(index, 1);
});

console.log(data.rows);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6:

data.rows.forEach((item, index) => item[0] === del && data.rows.splice(index, 1));

You can use lodash for filter your objects or arrays. Look at the filter method for your case:

var myObject = {
"headers":[[
{"text":"Code","class":"Code"},
{"text":"Code","class":"Code"}
]],
"rows":[
[0,"Peter", 51123123],
[3,"John", 90909090],
[5,"Mary",51123123]
],
"config":[[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
"other":[[13,0]]
};

//filter by lodash
myObject.rows =  _.filter(myObject.rows,function(row){
  return row[0] !== 3;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796021a4594206.html

相关推荐

  • javascript - splice row from array by value - Stack Overflow

    I want splice the line with value = 3[3,"John", 90909090]data.json{"headers":[[{&

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信