0

I need to append different key elements of objects in an array.

I have:

["2022-12-01", "2022-12-02", "2022-12-03", "2022-12-04"]

I need an array like this:

[{
    "2022-12-01": {
      disableTouchEvent: true,
      "selected": true,
      "selectedColor": "#ffff",
      "selectedTextColor": "grey"
    }
  },
  {
    "2022-12-02": {
      disableTouchEvent: true,
      "selected": true,
      "selectedColor": "#3634A3",
      "selectedTextColor": "#ffff"
    }
  },
  {
    "2022-12-03": {
      disableTouchEvent: true,
      customStyles: customDateStyle2,
      "selected": true,
      "selectedColor": "#ffff",
    }
  },
  {
    "2022-12-04": {
      disableTouchEvent: true,
      customStyles: customDateStyles,
      "selected": true,
      "selectedColor": "#ffff",
    }
  }
]
4
  • 1
    Where is all that other data coming from? Commented Dec 12, 2022 at 7:35
  • What have you tried? Array.map? Commented Dec 12, 2022 at 7:36
  • Data is dynamic Commented Dec 12, 2022 at 7:37
  • So did my solution answer your question? @harika Commented Dec 14, 2022 at 19:35

2 Answers 2

2

Am not sure if I'm getting the original OP's throughs, but the code can be implemented this way

    let dates = ["2022-12-01", "2022-12-02", "2022-12-03", "2022-12-04"];
    
    
    let details = [{
          disableTouchEvent: true,
          "selected": true,
          "selectedColor": "#ffff",
          "selectedTextColor": "grey"
        },
      {
          disableTouchEvent: true,
          "selected": true,
          "selectedColor": "#3634A3",
          "selectedTextColor": "#ffff"
        }
     ,
     {
          disableTouchEvent: true,
          customStyles: "customDateStyle2",
          "selected": true,
          "selectedColor": "#ffff",
       
      },
       {
          disableTouchEvent: true,
          customStyles: "customDateStyles",
          "selected": true,
          "selectedColor": "#ffff",
        
      }
      ]
    
    let result = dates.map((v, i)=> ({
         [v]: details[i]   
        }) )
    
    console.log(result)

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

Comments

0

I think mapping dates will do what you want

like this :

const dates = ["2022-12-01", "2022-12-02", "2022-12-03", "2022-12-04"]

   let data = dates.map((date,index)=> ({
     [date]:dataFromBackend[index]   
    }) )

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.