简体   繁体   English

如何过滤嵌套在数组对象属性中的对象数组?

[英]How do i filter array of objects nested in property of array objects?

I have model like this: 我有这样的模型:

var model = [{id: 1, prices: [{count: 2}, {count: 3}]}, {id: 2, prices: [{count: 2}]}, {id: 3, prices: [{count: 3}]}];

and I need to filter this objects of array useing property count and I will need to return matched objects in three scenarios: 并且我需要使用属性count来过滤此数组对象,并且需要在三种情况下返回匹配的对象:

  1. if the objects have two objects in array prices , 如果对象在prices数组中有两个对象,
  2. if the objects have one object in array prices matching count:2 , 如果这些对象在数组prices具有一个与count:2相匹配的对象,
  3. if the objects have one property in array prices matching count:3 . 如果对象在数组prices具有一个属性,匹配count:3

so..when i click the button without assigned value i wanna see all objects, when i click button with value = 2 i wanna see objects with count: 2 and when i click the button with value = 3 i wanna get objects with count: 3, i must do this in AngularJS – 所以..当我单击没有赋值的按钮时,我想查看所有对象,当我单击具有值= 2的按钮时,我想查看具有计数:2的对象,当我单击具有值= 3的按钮时,我想获取具有计数的对象: 3,我必须在AngularJS中执行此操作–

maybe something like this? 也许像这样?

var result = model.filter(function(m) {
   // make sure the m.prices field exists and is an array
   if (!m.prices || !Array.isArray(m.prices)) {
       return false;
   }

   var numOfPrices = m.prices.length

   if (numOfPrices === 2) { // return true if its length is 2
       return true;
   }

   for (var i = 0; i < numOfPrices; i++) {
       if (m.prices[i].count && 
           (m.prices[i].count === 2 || 
            m.prices[i].count == 3)) {
            return true;
        }
    }

    return false;
});

use lodash or underscore library.. and then your code with lodash will be like: 使用lodash或下划线库..然后,使用lodash的代码将类似于:

_.filter(model, function(i){
   return _.intersection(_.map(i.prices, 'count'), [3,2]).length;
})

it returns items that on their price property have array which contains element with count = 3 or count = 2 它返回在其price属性上具有包含count = 3或count = 2元素的数组的项目

var model = [{
    id: 1,
    prices: [{
        count: 2
    }, {
        count: 3
    }]
}, {
    id: 2,
    prices: [{
        count: 2
    }]
}, {
    id: 3,
    prices: [{
        count: 3
    }]
}];

var search = function(data) {
    var result = {};

    function arrayObjectIndexOf(myArray, searchTerm, property) {
        for (var i = 0, len = myArray.length; i < len; i++) {
            if (myArray[i][property] === searchTerm) return i;
        }
        return -1;
    }

    for (var index in data) {
        if (data[index].hasOwnProperty("prices") && arrayObjectIndexOf(data[index].prices, 2, 'count') != -1) {
            result[data[index].id] = data[index];
        } else if (data[index].hasOwnProperty("prices") && arrayObjectIndexOf(data[index].prices, 3, 'count') != -1) {
            result[data[index].id] = data[index];
        } else if (data[index].hasOwnProperty("prices") &&
            data[index].prices.length == 2) {
            result[data[index].id] = data[index];
        }

    }

    return result;
}
var output = search(model);
console.log(output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM