简体   繁体   English

根据属性名称和类型对对象进行分组

[英]Group objects based on property name and type

I have an array as per below; 我有一个数组如下

var myArray = {
    "cartItems": {"paramA1": 25, "paramA2": 35},
    "cartShippingCost": {"paramB1": 4, "paramB2": 152, "paramB3": 536, "paramB4": 56},
    "cartNo": 675765,
    "cartSpecialRequirements": {"paramC1": 432},
    "cartPostage": {"paramD1": 56, "paramD2": 6537},
    "cartId": 54,
    "tel": 7778798548,
    "status": 5465465,
    "delivery": 65464646686674
};

var cartItems;

I perform a loop over myArray to find the objects who' key contains 'cart' 我在myArray上执行循环以查找键包含“购物车”的对象

for (var key in myArray) {
    if (myArray.hasOwnProperty(key)) {
        if (key.indexOf('cart') > -1) {
            alert(key + " -> " + o[key]);
        }
    }
}

Which works great. 哪个很棒。 I want to retrieve objects: 我想检索对象:

  • with keys containing 'cart' 包含“购物车”的钥匙
  • and is of type structure, egfrom the above, cartNo and cartId are NOT of type structures. 并具有类型结构,例如,从上面看, cartNocartId不是类型结构。
  • push the 'cart' structures type in to cartItems; 将“购物车”结构类型推入cartItems;

like this? 像这样?

var data = {
    "cartItems": {"paramA1": 25, "paramA2": 35},
    "cartShippingCost": {"paramB1": 4, "paramB2": 152, "paramB3": 536, "paramB4": 56},
    "cartNo": 675765,
    "cartSpecialRequirements": {"paramC1": 432},
    "cartPostage": {"paramD1": 56, "paramD2": 6537},
    "cartId": 54,
    "tel": 7778798548,
    "status": 5465465,
    "delivery": 65464646686674
}

var cartObjects = [];
for(var k in data){
    var v = data[k];
    if(v && typeof v === "object" && k.indexOf("cart") !== -1 && data.hasOwnProperty(k)){
        cartObjects.push(v);
    }
}

console.log(cartObjects)

You can check if a variable is an object (structure as you said) by using typeof(var) . 您可以使用typeof(var)来检查变量是否为对象(如您所说的结构typeof(var) In your case, you could do something like this in your loop : 就您而言,您可以在循环中执行以下操作:

if (typeof(myArray[key]) == 'object')
{
    // do something
}

Hope it helps! 希望能帮助到你!

You just need to first filter your object for keys which contain 'cart' and they reference properties of type 'object'. 您只需要首先针对包含“购物车”且其引用了“对象”类型属性的键过滤对象。 Then map them to the referenced object. 然后将它们映射到引用的对象。 This will result in an array of objects. 这将导致对象数组。

var cartItems = Object.keys(myArray)
    .filter(key => {
        return key.indexOf('cart') > -1 && typeof myArray[key] === 'object';
    })
    .map(key => {
        return myArray[key];
    });

You may do it like this 你可以这样

 var myArray = { "cartItems": {"paramA1": 25, "paramA2": 35}, "cartShippingCost": {"paramB1": 4, "paramB2": 152, "paramB3": 536, "paramB4": 56}, "cartNo": 675765, "cartSpecialRequirements": {"paramC1": 432}, "cartPostage": {"paramD1": 56, "paramD2": 6537}, "cartId": 54, "tel": 7778798548, "status": 5465465, "delivery": 65464646686674 }; var result = Object.keys(myArray).reduce(function (p, c) { var value = myArray[c]; if (c === "cartItems" || c.indexOf("cart") < 0) { return p; } if (["string", "number", "boolean"].indexOf(typeof value) > -1) { p.push(value); } return p; }, []); document.write(JSON.stringify(result)); 

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

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