Extract specific values from key/value

I have a json object like:

{
"hrMl": [
  {
        "key": "mlHr",
         "data": 1,
  },
  {
         "key": "mlHr",
           "data": 2,
  },
  {
        "key": "mlHr",
         "data": 2,
  },
 ]
}

I’m wondering how I can extract data where it equals 2 and put to an array and 1 into an array?

So the result will be two new arrays:

TArray = [2,2]
OArray = [1]

Here is a jsfiddle of what I am trying to do.
Thanks so much!

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Thanks so much! I can update my post with an example.

You can’t use .map here, .map is a method that takes an array, changes each (or some) items, and returns a new array with the same length as the original array.

Instead of .map, you might want to look into a different method called .filter:

1 Like

Thank you!
Based off the docs for .filter I put together:

const hr = [
  {
        "key": "mlHr",
         "data": 1,
  },
  {
         "key": "mlHr",
           "data": 2,
  },
  {
        "key": "mlHr",
         "data": 2,
  },
 ]
 
data1 = [];
data2 = [];
function filterByID(item) {
  if (item.data == 2) {
    data2.push(item)
  }
  if (item.data == 1) {
    data1.push(item)
  }
  
}

let arrByID = hr.filter(filterByID)

const datas1 = data1.map(({ data }) => data);
const datas2 = data2.map(({ data }) => data);
console.log(datas1);
console.log(datas2);
1 Like

You could also do something like the following to reduce repetition.

const hr = [{
    "key": "mlHr",
    "data": 1,
  },
  {
    "key": "mlHr",
    "data": 2,
  },
  {
    "key": "mlHr",
    "data": 2,
  },
]

const createArr = (arr, val) => {
  return arr.reduce((arr, { data }) => {
    return data === val ? arr.concat(val) : arr;
  }, []);
};

const tArray = createArr(hr, 2);
const oArray = createArr(hr, 1);
console.log(tArray); // [2, 2]
console.log(oArray); // [1]

If the only two values for data are 1 and 2, you could simplify even more.

1 Like

Thanks so much! This is great!!

Out of curiosity, how will the two arrays be used?

As series data for a bar graph.

For ex. jsfiddle from Highcharts Forum

But instead of just one array with all values, I have a json object.

I guess technically I could make a giant array out of all the "data" keys using map and then follow the HighCharts jsfiddle example to divide data to different arrays.

Something like:

const hr = [{
    "key": "mlHr",
    "data": 1,
  },
  {
    "key": "mlHr",
    "data": 2,
  },
  {
    "key": "mlHr",
    "data": 2,
  },
  {
    "key": "mlHr",
    "data": 3,
  },
]

const allData = hr.map(({ data }) => data);

Highcharts.chart('container', {
  chart: {
    type: 'bar',
    events: {
      load: function() {
        var chart = this
        dataO = [],
          dataTw = [],
          dataTh = [];

        allData.forEach(function(point) {
          console.log(point)
          if (point === 1)
            dataO.push({
              x: 0,
              y: point
            })
          else if (point === 2)
            dataTw.push({
              x: 1,
              y: point
            })
          else if (point === 3)
            dataTh.push({
              x: 2,
              y: point
            })
        })

        chart.addSeries({
          data: dataO
        });
        chart.addSeries({
          data: dataTw
        });
        chart.addSeries({
          data: dataTh
        });
      }
    }
  },
...........

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.