0

I want to dynamically create as many variables as the number of color data read from json and assign the same color values ​​to the created variables.

For example; blue colors dynamically incoming variable red colors dynamically incoming variable green colors dynamically incoming variable

then I want to store them in different dynamic arrays

{

    "color": "red"
},  
{

    "color": "red"
},  
{

    "color": "green"
}, 
{

    "color": "blue"
} 
function datareadd(latitude, longitude, number) {
  $.getJSON("json.json", function(dataa) {
    var markers = [];
    var renkler = [];
    for (var i = 0; i < dataa.length; i++) {
      markers[i] = [];
      console.log(markers);
    }
    let grup = dataa.reduce((r, a) => {
      r[a.color] = [...r[a.color] || [], a];
      return r;
    }, renkler);

    console.log(grup.blue);
  })
}
datareadd();

1 Answer 1

1

You think you want to achieve like below.

var dataa = [{
    "color": "red"
  },
  {
    "color": "green"
  },
  {
    "color": "blue"
  }
];

var grup = dataa.map(x => ({ [x.color]: x.color }));

console.log(grup);

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

3 Comments

thanks for your answer why i want to keep the resulting color values ​​in different arrays automatically
What is your expected output array look like? Like this [ { "red" : "red" }, { "green" : "green" }, { "blue" : "blue" } ]. But for array you will not be able to get value like grup.blue.
yes I want it to look like this [ { "red" : "red" }, { "green" : "green" }, { "blue" : "blue" } ]

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.