简体   繁体   中英

foreach loop not working when iterating over object properties from an array of objects [javascript]

I have a variable 'layerArr' that contains and array of objects. each with their own properties.

I wish to loop through the 'absoluteEndM' property of every object to check to see if its value is greater than the value of sctrLength and then do something depending on the answer. I have tried the following:

       LayerArr[i].absoluteEndM.forEach(function (endMtr)
        {
            if (endMtr > sctrLength) {
                maxWidth = endMtr;
            }
            else {
                maxWidth = sctrLength;
            } 
        })

and

       for (var i = 0; i < LayerArr.length; i++) {
            if (LayerArr[j].absoluteEndM > sctrLength) {
                maxWidth = LayerArr[j].absoluteEndM;
            }
            else {
                maxWidth = sctrLength;
            } 
        }

neither work and I am not sure why. What is the correct way to do this? thanks

EDIT: layerArr contains the following objects:

layerArr[0], LayerArr[1], LayerArr[2] and LayerArr[3] (they are just numbered, no name)

each of those have a bunch of properties for example:

layerArr[0].endDate
layerArr[0].cracked
layerArr[0].absoluteEndM
etc...

and:

layerArr[1].endDate
layerArr[1].cracked
layerArr[1].absoluteEndM
etc...
const layerArr = [
    { absoluteEndM: 2 },
    { absoluteEndM: 4 },
    { absoluteEndM: 1 }
]

const sctrLength = 3;
let maxWidth = 0;

for (let layer of layerArr) {
    maxWidth = layer.absoluteEndM > sctrLength ? layer.absoluteEndM : sctrLength
    console.log(maxWidth)
}

This snippet should work (ES6, you might have to transpile it to ES5 or do some work yourself).

But the functionality of the code itself is a little weird, you actually get one value in maxWidth which isn't representative. You can make an array like this:

const maxWidths = layerArr.map(layer => layer.absoluteEndM > sctrLength ? layer.absoluteEndM : sctrLength)
console.log(maxWidths)

A demonstration:

 const layerArr = [ { absoluteEndM: 2 }, { absoluteEndM: 4 }, { absoluteEndM: 1 } ] const sctrLength = 3; let maxWidth = 0; // Start of example 1 - just setting the maxWidth for (let layer of layerArr) { maxWidth = layer.absoluteEndM > sctrLength? layer.absoluteEndM: sctrLength console.log(maxWidth) } // End of example 1 // Start of example 2 - making an array of maxWidths const maxWidths = layerArr.map(layer => layer.absoluteEndM > sctrLength? layer.absoluteEndM: sctrLength) console.log(maxWidths) // End of example 2

It's a little tricky to tell exactly what you need from your question, but it seems that what you're looking to do is ascertain the max value for absoluteEndM from an array of objects, but also including the value of sctrLength . Here's how that might look:

 const LayerArr = [ { absoluteEndM: 2, }, { absoluteEndM: 4, }, { absoluteEndM: 1, }, ] const sctrLength = 3 const maxWithDefault = defaultValue => items => Math.max(...[...items, defaultValue]) const maxIncludingSctrLength = maxWithDefault(sctrLength) const path = k => x => x[k] const getAbsoluteEndM = path('absoluteEndM') const getMaxAbsoluteEndM = _layers => maxIncludingSctrLength(_layers.map(getAbsoluteEndM)) const result = getMaxAbsoluteEndM(LayerArr) console.dir(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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