最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove an object from an array on a condition - Stack Overflow

programmeradmin11浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

You 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.

发布评论

评论列表(0)

  1. 暂无评论