I have an array with a list of objects:
var myArray = [
{"cartItems": {"paramA1": 25, "paramA2": 35}},
{"cartShippingCost": {"paramB1": 4, "paramB2": 152, "paramB3": 536, "paramB4": 56}},
{"cartSpecialRequirements": {"paramC1": 432}},
{"cartPostage": {"paramD1": 56, "paramD2": 6537}},
{"uid": {"paramE1": 545}},
{"tel": 7778798548}
];
How can i loop through the above and group objects that contain 'cart' and ones that don't?
I know i need to create 2 temp obj's e.g.
var cartObj;
var nonCartObj;
And perform a .push in to each of these if the condition is met.
Tried:
for(var i in myArray) {
if (i == 'cart') {
console.log('cart match found');
}
}
Update:
Using Object.keys always hitting else case:
var key = Object.keys(item);
if (key.indexOf("cart") !== -1) {
alert(key + " contains cart");
} else {
alert(key + " doesnt contain cart");
}