3

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 resultsI 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, i.e. 494927, 48883, 59976, 1205915, 1434262

2
  • 1
    Change it to arrCombined.map(function(e) and e.result.replace Commented May 27, 2020 at 14:50
  • 1
    But, that code should throw: Uncaught TypeError: e.replace is not a function Commented May 27, 2020 at 14:56

4 Answers 4

2

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

It should be like this.

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.