0

I am trying extract an array that is inside an object in an array and I want to edit the value of the objects within that extracted array. It seems like this should be simple but I cant figure out why my forEach isnt updating the objects. From what I understand forEach mutates the original array so why isnt this working?

My expected result would be:


      [
          {
            "label": "400 mg",
            "value": "400 mg",
          },
          {
             "label": "600 mg",
             "value": "600 mg",
          },
          {
             "label": "800 mg",
             "value": "800 mg",
          }
      ]

const array1 = [
        {
            "Name": "IBU (oral - tablet)",
            "Strength": [
                {
                    "Strength": "400 mg",
                    "NDC": "55111068201"
                },
                {
                    "Strength": "600 mg",
                    "NDC": "55111068301"
                },
                {
                    "Strength": "800 mg",
                    "NDC": "55111068401"
                }
            ]
        },          
    ];

let newA = array1.map((item) => item.Strength).flatMap((item) => item)

newA.forEach((str) => ({label: str.Strength ,value: str.Strength}))


console.log(newA)

3
  • The forEach method executes a provided function once for each array element without having any return. In your forEach you create a new object which does not get used nor returned. newA.forEach((item) => {item.label = item.Strength}) would change the initial object. Commented Sep 15, 2022 at 14:23
  • 1
    You should not try to use .forEach method on arrays, you are looking for developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - with this you can achieve your desired result. Commented Sep 15, 2022 at 14:23
  • @NicolaeMaties I see what you mean I was trying not to use .map method over an over. I could just map over the newArr again but I was thinking there might be a better way that doesnt involve creating a new copy of the array. Commented Sep 15, 2022 at 14:32

2 Answers 2

1

I assume that you are looking for this, using .map not .forEach.

const result = [
        {
            "Name": "IBU (oral - tablet)",
            "Strength": [
                {
                    "Strength": "400 mg",
                    "NDC": "55111068201"
                },
                {
                    "Strength": "600 mg",
                    "NDC": "55111068301"
                },
                {
                    "Strength": "800 mg",
                    "NDC": "55111068401"
                }
            ]
        }    
][0].Strength.map(el => ({ label: el.Strength, value: el.Strength }));

console.log(result);

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

Comments

0

const array1 = [
        {
            "Name": "IBU (oral - tablet)",
            "Strength": [
                {
                    "Strength": "400 mg",
                    "NDC": "55111068201"
                },
                {
                    "Strength": "600 mg",
                    "NDC": "55111068301"
                },
                {
                    "Strength": "800 mg",
                    "NDC": "55111068401"
                }
            ]
        },          
    ];

let newA = array1.map((item) => item.Strength).flatMap((item) => item)

newA.map((str) => {
  str.label= str.Strength ;
  str.value = str.Strength;
  delete str.Strength;
  delete str.NDC
})
console.log(newA)

1 Comment

This returns the same value as my attempt. I see what youre trying to do. I think you forgot to assign the second map a var name.

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.