In my application, $scope.results = [{},{},{},{}.....].(Is an Array containing multiple objects)
Sample Output from one of the Objects:
0:
brand: "Confidential"
group_id: "CRCLEBESP"
id: "d703187ac59976b066c9b7ea01416bca"
msrp: "0"
name: "Clear Beyond Speaker Cable"
price: "5510"
product_type_unigram: "cable"
sku: "CRCLEBESP"
ss_msrp: (12) ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
ss_msrp_max: "0"
ss_msrp_min: "0"
ss_name_sizes: "Cardas Clear Beyond Speaker Cable"
ss_price: (12) ["6840", "5510", "9500", "8170", "8170", "12160", "10830", "14820", "13490", "17480", "16150", "18810"]
ss_price_max: "18810"
ss_price_min: "5510"
stock_id: "171038"
sub_sku: (12) ["1.5PSPSP", "1PSPSP", "2.5PSPSP", "2PBNBN", "2PSPSP", "3.5PSPSP", "3PSPSP", "4.5PSPSP", "4PSPSP", "5.5PSPSP", "5PSPSP", "6PSPSP"]
uid: "171038"
__proto__: Object
Those objects contain 3 arrays each that I need to pull values from called "ss_msrp , ss_price, and sub_sku". Each array in each object is the same length. For example, Object 0 has 3 arrays, each array is 12 indexes long. Object 1 has 3 arrays, each array is 6 indexes long. Object 3 has 3 arrays, each array is 8 indexes long etc.
I need to pull the [n]th item from each array and place it into a new object. Each object will have 3 key-value pairs. For example:
ss_msrp = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
ss_price = ["6840", "5510", "9500", "8170", "8170", "12160", "10830", "14820", "13490", "17480", "16150", "18810"]
sub_sku = ["1.5PSPSP", "1PSPSP", "2.5PSPSP", "2PBNBN", "2PSPSP", "3.5PSPSP", "3PSPSP", "4.5PSPSP", "4PSPSP", "5.5PSPSP", "5PSPSP", "6PSPSP"]
object1 = { msrp: "0", price: "6840", sku: "1.5PSPSP" }
object2 = { msrp: "0", price: "5510", sku: "1PSPSP" }
object3 = { msrp: "0", price: "9500", sku: "2.5PSPSP" }
object4 = { msrp: "0", price: "8170", sku: "2PBNBN" }
My code right now looks like this:
let objects = []
for (let i=0; i < $scope.results[0].sub_sku.length; i++){
objects.push({
sku: $scope.results[0].sub_sku[i],
msrp: $scope.results[0].ss_msrp[i],
price: $scope.results[0].ss_price[i]
})
}
This of course will only return the desired results for the first object. I have tried nesting this inside another for loop like this:
for (let x=0; x < $scope.results.length; x++){
for (let i=0; i < $scope.results[x].sub_sku.length; i++){
objects.push({
sku: $scope.results[x].sub_sku[i],
msrp: $scope.results[x].ss_msrp[i],
price: $scope.results[x].ss_price[i]
})
}
}
but I am getting the following error: Cannot read property 'length' of undefined
Anyone have any ideas? Thank you in advance.