0

how to change value of the object to an object contains key:value key:value to all models array if there any way please share it with me

const hello = [
    {
      brand: "Acura",
      models: [
        "2.2CL",
        "2.3CL",
        "3.0CL",
        "3.2CL",
        "ILX",
        "Integra",
        "Legend",
        "MDX",
        "NSX",
        "RDX",
        "3.5 RL",
        "RL",
        "RSX",
        "SLX",
        "2.5TL",
        "3.2TL",
        "TL",
        "TSX",
        "Vigor",
        "ZDX"
      ]
    },
    {
      brand: "Alfa Romeo",
      models: [
        "164",
        "8C Competizione",
        "GTV-6",
        "Milano",
        "Spider"
      ]
    },
    {
      brand: "AMC",
      models: [
        "Alliance",
        "Concord",
        "Eagle",
        "Encore",
        "Spirit"
      ]
    },

and make it like this for all models {value:"value", label:"value"},

const hello = [
    {
      brand: "Acura",
      models: [
       {value:"2.2CL", label:"2.2CL"},
       {value:"2.3CL", label:"2.3CL"},
       .



       

and make it like this for all models {value:"value", label:"value"},and make it like this for all models {value:"value", label:"value"},and make it like this for all models {value:"value", label:"value"},

0

2 Answers 2

4

Here is what you could do :

const hello = [{
    brand: "Acura",
    models: [
      "2.2CL",
      "2.3CL",
      "3.0CL",
      "3.2CL",
      "ILX",
      "Integra",
      "Legend",
      "MDX",
      "NSX",
      "RDX",
      "3.5 RL",
      "RL",
      "RSX",
      "SLX",
      "2.5TL",
      "3.2TL",
      "TL",
      "TSX",
      "Vigor",
      "ZDX"
    ]
  },
  {
    brand: "Alfa Romeo",
    models: [
      "164",
      "8C Competizione",
      "GTV-6",
      "Milano",
      "Spider"
    ]
  },
  {
    brand: "AMC",
    models: [
      "Alliance",
      "Concord",
      "Eagle",
      "Encore",
      "Spirit"
    ]
  },
];

const result = hello.map(item => {
  return {
    ...item,
    models: item.models.map(val => {
      return {
        value: val,
        label: val
      }
    })
  }
});

console.log(result)

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

3 Comments

thank you what about if i want also brand to make it like this brand:{value:"bmw",label:"bmw"}
@Peterrabbit thank you what about if i want also brand to make it like this brand:{value:"bmw",label:"bmw"} –
@amghare then just add brand: {value: item.brand, label: item.brand}, and if there are no other fields than model and brand you can remove the ...item .
1

Maybe this is not the best answer, but the map and reduce methods is what you are looking for

const result = hello.map((item) => {
  const models = item.models.reduce((acum, curr) => {
    acum.push({
      value: curr,
      label: curr,
    });

    return acum;
  }, []);

  return {
    brand: item.brand,
    models,
  };
});

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.