I have a javascript array object. I want to find the result of a single array. The following code does what I want but always iterates over the whole array. How can I achieve the same on just one array?
b = [];
b[0] = {};
b[0].pts = [1,4,8,12,1];
b[0].result = 0;
b[1] = {};
b[1].pts = [2,1,3,2,10,12];
b[1].result = 0;
b[2] = {};
b[2].pts = [5,1,9];
b[2].result = 0;
/* Iterates over entire object */
b.forEach(
e=>e.pts.forEach((c,i)=>{
if(i && c<=4){
e.result += c
};
if(e.pts[0]<=3 && i==0){
e.result+=e.pts[0];
};
})
);
console.log(">> "+b[0].result+"<br/>");
console.log(">> "+b[1].result+"<br/>");
console.log(">> "+b[2].result+"<br/>");
/* Iterate over a single object */
Thanks.