1

内部にいくつかの項目を含む配列があり、そのうちの 1 つは場所です。これらの場所の一部は内部が空で、値がありません。これらの空の場所をすべて取り、関数を実行したいと思います。その方法を知っている人はいますか?

配列は次のようになります。

array=[{user:a,user_id:b,date:c,profile_img:d,text:e,contentString:f,url:g,location:""},    
{user:a,user_id:b,date:c,profile_img:d,text:e,contentString:f,url:g,location:""}];
4

3 に答える 3

0

ループを作成することもできますが、優れたフィルター機能を持つ Underscore.js を使用する方がはるかに簡単です。

場所が空のユーザーを返すテストを作成するだけです。

var allUsers = [{
    user: "user-with-location",
    user_id: "b",
    date: "c",
    profile_img: "d",
    text: "e",
    contentString: "f",
    url: "g",
    location: "asdf"
}, {
    user: "user-without-location",
    user_id: "b",
    date: "c",
    profile_img: "d",
    text: "e",
    contentString: "f",
    url: "g",
    location: ""
}];

var usersWithoutLocation = _.filter(allUsers, function(user) {
    return user.location === "";
});
于 2012-08-18T22:16:59.013 に答える
0

そしてヒントとして、値のチェックを簡素化できます。

for (var i = 0; i < array.length; i++) {
  if (!array[i].location) {
    //Do something with array[i]
  }
}
于 2012-08-19T00:20:56.660 に答える
0

単純なforループが機能するはずです。

for (var i = 0; i < array.length; i++) {
    if (array[i].location.length == 0) {
        //Do something with array[i]
    }
}
于 2012-08-18T21:07:21.260 に答える