简体   繁体   中英

Javascript - mapping array of objects undefined

I am passing an object from a function that contains an array arrCombined . I have an object titled results that I would like to map and remove the strings to so I can convert these strings into an integer. When mapping my array of objects for results I am stuck on getting undefined.

Here is my array:

[..]
    0: Object { result: "494,927", risk: "LOW", sector: "Online" }
    ​
    1: Object { result: "48,883", risk: "MEDIUM-LOW", sector: "Retail Stores" }
    ​
    2: Object { result: "59,976", risk: "MEDIUM-LOW", sector: "Store Pick up" }
    ​
    3: Object { result: "1,205,915", risk: "MEDIUM", sector: "Drive in" }
    ​
    4: Object { result: "1,434,262", risk: "LOW", sector: "In store" }

I am declaring a variable finalResult to return the targeted "result" in my mapping function which looks like this.

​ let finalResult = arrCombined.arrCombined.result.map(function (e) {
        return Number(e.replace(/(,\s*)+/g, '').trim());
    });

console.log(finalResult) // undefined.

I am expecting a finalResult to return the result objects as numbers, ie 494927, 48883, 59976, 1205915, 1434262

You need to take result property from each object.

 var arrCombined = [ { result: "494,927", risk: "LOW", sector: "Online" }, { result: "48,883", risk: "MEDIUM-LOW", sector: "Retail Stores" }, { result: "59,976", risk: "MEDIUM-LOW", sector: "Store Pick up" }, { result: "1,205,915", risk: "MEDIUM", sector: "Drive in" }, { result: "1,434,262", risk: "LOW", sector: "In store" } ], finalResult = arrCombined.map(({ result }) => Number(result.replace(/(,\s*)+/g, '').trim())); console.log(finalResult);

This happens because you are trying to access result property from arrCombined which is undefined and thus your code is not working. You simply need to map through arrCombined and then access result property of each object inside array like:

 const arrCombined = [ { result: "494,927", risk: "LOW", sector: "Online" }, { result: "48,883", risk: "MEDIUM-LOW", sector: "Retail Stores" }, { result: "59,976", risk: "MEDIUM-LOW", sector: "Store Pick up" }, { result: "1,205,915", risk: "MEDIUM", sector: "Drive in" }, { result: "1,434,262", risk: "LOW", sector: "In store" } ] let finalResult = arrCombined.map(function (e) { return Number(e.result.replace(/(,\s*)+/g, '').trim()); }); console.log(finalResult)

It should be like this.

let finalResult = arrCombined.arrCombined.map(function (e) {
        return Number(e.result.replace(/(,\s*)+/g, '').trim());
    });

It should solve your issue

 var arrCombined = [ { result: "494,927", risk: "LOW", sector: "Online" }, { result: "48,883", risk: "MEDIUM-LOW", sector: "Retail Stores" }, { result: "59,976", risk: "MEDIUM-LOW", sector: "Store Pick up" }, { result: "1,205,915", risk: "MEDIUM", sector: "Drive in" }, { result: "1,434,262", risk: "LOW", sector: "In store" } ] var finalResult = arrCombined.map(item => { return Number(item.result.replace(/(,\s*)+/g, '').trim()) }); console.log(finalResult)

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