admin管理员组文章数量:1328052
I have a csv file that I've already splitted by line breaks and I've broken it down even more by the mas in the line breaks to give me three things I'm looking for and named them for later use in the program. What I'm looking to do is remove an item if a one of the objects match a certain value.
var values=[];
var output="";
for(i = 0; i < csv_split.length; i++){
csv_split[i] = csv_split[i].split(',') //Splits the csv file that's already split by new line by mas
values[i]={}
values[i].Name=newline_split[i][1]; //finds the second object and names it values.name
values[i].Rev=newline_split[i][2]; //finds the third object and names it values.rev
values[i].URL=newline_split[i][9]; //finds the eighth object and names it values.url
}
This then is used later so I can get a list of just the values I'm looking for.
for (i=0; i < values.length; i++){
output += values[i].Name + ',';
output += values[i].Rev + ',';
output += values[i].URL+ ',';
output += "\n\n";
}
So what I did was modified this code to the first for loop:
if (values[i].Rev == "NO ACCESS") {
csv_split[i].splice(0,1);
}
The idea behind this was that if the values.Rev matched to "NO ACCESS" it would remove the entirety csv_split[i], so that later it wouldn't display it in the output.
Running the script now gives the entire output of regardless if values.Rev matches "NO ACCESS" or not. What am I missing with this?
I have a csv file that I've already splitted by line breaks and I've broken it down even more by the mas in the line breaks to give me three things I'm looking for and named them for later use in the program. What I'm looking to do is remove an item if a one of the objects match a certain value.
var values=[];
var output="";
for(i = 0; i < csv_split.length; i++){
csv_split[i] = csv_split[i].split(',') //Splits the csv file that's already split by new line by mas
values[i]={}
values[i].Name=newline_split[i][1]; //finds the second object and names it values.name
values[i].Rev=newline_split[i][2]; //finds the third object and names it values.rev
values[i].URL=newline_split[i][9]; //finds the eighth object and names it values.url
}
This then is used later so I can get a list of just the values I'm looking for.
for (i=0; i < values.length; i++){
output += values[i].Name + ',';
output += values[i].Rev + ',';
output += values[i].URL+ ',';
output += "\n\n";
}
So what I did was modified this code to the first for loop:
if (values[i].Rev == "NO ACCESS") {
csv_split[i].splice(0,1);
}
The idea behind this was that if the values.Rev matched to "NO ACCESS" it would remove the entirety csv_split[i], so that later it wouldn't display it in the output.
Running the script now gives the entire output of regardless if values.Rev matches "NO ACCESS" or not. What am I missing with this?
Share Improve this question edited Mar 29, 2016 at 16:37 Michael 44.3k12 gold badges97 silver badges141 bronze badges asked Mar 29, 2016 at 16:05 Taco-BolasTaco-Bolas 311 gold badge2 silver badges4 bronze badges 2- You mean to remove the whole CSV line, when the condition is not met? – Roman Hocke Commented Mar 29, 2016 at 16:10
- I want to remove the entire line of csv_split whenever the condition is met. For example if in csv_split[1] the value of values[1].Rev is equal to "NO ACCESS" I want to remove all the values from csv_split[1] so that later when it is brought up in the second code box it won't show any of the lines where values.Rev = "NO ACCESS" – Taco-Bolas Commented Mar 29, 2016 at 16:14
1 Answer
Reset to default 4You could do this much easier with the filter method. You use it like this:
var finalArr = values.filter(function(val) { return val.Rev != "NO ACCESS"; });
This should give you an array of everything that has access.
本文标签: javascriptRemove an object from an array on a conditionStack Overflow
版权声明:本文标题:javascript - Remove an object from an array on a condition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742241633a2438872.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论