如何找到特定的对象并将它们放在javascript中的数组中

时间:2021-01-14 13:38:47

I have this result in my script

我的脚本中有这个结果

'[{"region":"NCA","depprt":"Havana, Cuba"},{"region":"NCA","depprt":"Havana, Cuba"},{"region":"NCA","depprt":"Montego Bay, Jamaica"},{"region":"NCA","depprt":"Montego Bay, Jamaica"}]'

this is the code to get it.

这是获得它的代码。

var jsonList = '@Html.Raw(Json.Encode(ViewBag.chk))'
        var jsList = JSON.stringify(jsonList);

for jsList I got above result.now I want to get all depprt where region is equal to NCA.how can I do that.

对于jsList我得到了结果。现在我想得到所有depprt,其中region等于NCA。我可以这样做。

5 个解决方案

#1


2  

You can use the .filter() method for this.

您可以使用.filter()方法。

var ncaList = jsonList.filter(function(obj){ return obj.region == "NCA"; });

#2


1  

Very simple. Iterate over the jList array and see if the region property matches your condition or not then append the item to your filtered array.

很简单。迭代jList数组并查看region属性是否与您的条件匹配,然后将该项追加到过滤后的数组中。

var filtered = [];
jList.forEach(function(item) {
   if(item.region == 'NCA') {
      filtered.push(item);
   }
});

#3


1  

Just iterate over it:

只是迭代它:

var filteredDepprts = [];
jsList.forEach(function(element){
  if(element.region == 'NCA'){
    filteredList.push(element.depprt); //or element if you want to push the full object
  }
});

#4


1  

The JSON.stringify method converts a JavaScript value to a string.

JSON.stringify方法将JavaScript值转换为字符串。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

When you want to convert a JSON string to a JavaScript value, use JSON.parse

如果要将JSON字符串转换为JavaScript值,请使用JSON.parse

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

var jsonList = '@Html.Raw(Json.Encode(ViewBag.chk))'
var jsList = JSON.parse(jsonList);

Using single quotes around your @Html.Raw, creates a string and not a JavaScript value. The filter method does not work on strings

在@ Html.Raw周围使用单引号创建一个字符串而不是JavaScript值。过滤器方法不适用于字符串

Eventually you could use Array.prototype.filter Filter out each element in array, that matches your criteria.

最终你可以使用Array.prototype.filter过滤掉符合条件的数组中的每个元素。

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

#5


1  

Try map:

var obj= [];
for (i in jsonList) {
  if (jsonList[i].region == "NCA") { obj.push(jsonList[i])};
}

https://jsfiddle.net/pd6hvn78/

#1


2  

You can use the .filter() method for this.

您可以使用.filter()方法。

var ncaList = jsonList.filter(function(obj){ return obj.region == "NCA"; });

#2


1  

Very simple. Iterate over the jList array and see if the region property matches your condition or not then append the item to your filtered array.

很简单。迭代jList数组并查看region属性是否与您的条件匹配,然后将该项追加到过滤后的数组中。

var filtered = [];
jList.forEach(function(item) {
   if(item.region == 'NCA') {
      filtered.push(item);
   }
});

#3


1  

Just iterate over it:

只是迭代它:

var filteredDepprts = [];
jsList.forEach(function(element){
  if(element.region == 'NCA'){
    filteredList.push(element.depprt); //or element if you want to push the full object
  }
});

#4


1  

The JSON.stringify method converts a JavaScript value to a string.

JSON.stringify方法将JavaScript值转换为字符串。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

When you want to convert a JSON string to a JavaScript value, use JSON.parse

如果要将JSON字符串转换为JavaScript值,请使用JSON.parse

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

var jsonList = '@Html.Raw(Json.Encode(ViewBag.chk))'
var jsList = JSON.parse(jsonList);

Using single quotes around your @Html.Raw, creates a string and not a JavaScript value. The filter method does not work on strings

在@ Html.Raw周围使用单引号创建一个字符串而不是JavaScript值。过滤器方法不适用于字符串

Eventually you could use Array.prototype.filter Filter out each element in array, that matches your criteria.

最终你可以使用Array.prototype.filter过滤掉符合条件的数组中的每个元素。

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

#5


1  

Try map:

var obj= [];
for (i in jsonList) {
  if (jsonList[i].region == "NCA") { obj.push(jsonList[i])};
}

https://jsfiddle.net/pd6hvn78/