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

javascript - Create new array from existing one only with elements that contain specific value - Stack Overflow

programmeradmin5浏览0评论

I create a new array from an existing one (big array with 100.000 objects). In the new array I want only elements where the value of "city" is for example New York City.

    var newData = [];

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

        if(data[i].city === "New York City") {

            newData[i] = {"city": data[i].city, "longitude": 
            data[i].longitude, "latitude": data[i].latitude, "state": 
            data[i].state};

        }
     }

I must be doing something wrong since a lot of elements in the new array are null

The new array then looks something like this:

[null,null,null,null,null, {"city":"New York", "logitude": 
-73.935242, "latitude": 40.730610, "state": "NY"},
null,null,null,null,null,null,"city":"New York", "logitude": 
-73.935242, "latitude": 40.730610, "state": "NY"}]

What am I doing wrong? How could I achieve my goal?

Thanks in advance guys!

I create a new array from an existing one (big array with 100.000 objects). In the new array I want only elements where the value of "city" is for example New York City.

    var newData = [];

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

        if(data[i].city === "New York City") {

            newData[i] = {"city": data[i].city, "longitude": 
            data[i].longitude, "latitude": data[i].latitude, "state": 
            data[i].state};

        }
     }

I must be doing something wrong since a lot of elements in the new array are null

The new array then looks something like this:

[null,null,null,null,null, {"city":"New York", "logitude": 
-73.935242, "latitude": 40.730610, "state": "NY"},
null,null,null,null,null,null,"city":"New York", "logitude": 
-73.935242, "latitude": 40.730610, "state": "NY"}]

What am I doing wrong? How could I achieve my goal?

Thanks in advance guys!

Share Improve this question asked Jun 5, 2017 at 10:39 Unknown UserUnknown User 5252 gold badges9 silver badges19 bronze badges 3
  • 1 Why not use filter? – evolutionxbox Commented Jun 5, 2017 at 10:43
  • @evolutionxbox: Looks like they want to create new objects. – T.J. Crowder Commented Jun 5, 2017 at 10:43
  • You could try using console.log(data[i]) inside the if statement, to see what part of the object is being outputted. Excluding the null result, does the new array contain all the correct results? – WizardCoder Commented Jun 5, 2017 at 10:48
Add a ment  | 

2 Answers 2

Reset to default 5

The elements won't be null, they'll be missing (which shows up as undefined when you try to access them). The reason is that you're increasing i every time, even when you skip an entry.

To fix it, use push instead:

var newData = [];

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

    if (data[i].city === "New York City") {
        newData.push({
            "city": data[i].city,
            "longitude": data[i].longitude,
            "latitude": data[i].latitude,
            "state": data[i].state
        });
    }
}

If you want the two arrays to share objects, you could use filter instead:

var newData = data.filter(function(entry) {
    return entry.city === "New York City";
});

but if you want the new array to have new objects that are different from the originals, your for loop is fine.

You can use Array.prototype.filter method:

newData.filter(function (el) {
  return el.city === "New York City";
});

or if you need other filter parameter:

newData.filter(function (el) {
  return el.city === "New York City" && el.state === "NY" ;
});

This method is part of the new ECMAScript 5th Edition standard.

From documentation:

filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callback test are simply skipped, and are not included in the new array.

发布评论

评论列表(0)

  1. 暂无评论