-2

I have got list of arrays .

let arr = ["one","two"]

My trying code is:

arr.map(item=>{
  item
})

I want to convert array of sub-array

[
            {
                "one": [{
                        value: "one"
                    }, 
                ]
            },
            {
                "two": [{
                        value: "two"
                    }, 
                ]
            },
]

3 Answers 3

2

You can try with using Object.values().

const arr = ["one", "two"];

const result = Object.values(arr).map(e => {
  return {
    [e]: [{value: e}]
  }
});

console.log(result);

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

1 Comment

1

you can also do it like this

let arr = ["one","two"]

arr.map(orgValue => {
    [orgValue]: [
      {
        value: orgValue
      }
    ]
  };
);

1 Comment

0

A simple forEach loop would help

let arr = ["one", "two"];
var res = [];
arr.forEach(val => {
  res.push({
    [val]: [
      {
        value: val
      }
    ]
  });
});
console.log(res);

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.