0

i have problem with my javascript project. i have an array with value like this :

exmpArr = ["PX1","PX2","PX3"];

and i want to loop and push it to obj like this:

sections = [
            {
             rows: [
              { title: exmpArr[i], rowId: exmpArr[i] },
              ],
            },
           ];

the final value must like this:

sections = [
            {
             rows: [
              { title: "PX1", rowId: "PX1" },
              { title: "PX2", rowId: "PX2" },
              { title: "PX3", rowId: "PX3" },
              ],
            },
           ];

what should i do?

what i did is i put for loop inside the object and its not work

1 Answer 1

1

map returns a new array from the one you're mapping over. So you can immediately assign that array as the property value when you build your object.

const exmpArr = ['PX1', 'PX2', 'PX3'];

const sections = [
  {
    rows: exmpArr.map(el => {
      return { title: el, rowId: el };
    })
  }
];

console.log(sections);

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

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.