1

I've got two object arrays with float value and string for shorthanded category.

const summer = [
  { _id: 'cat1', Datasets: 496, Average: 27.586688911290324 },
  { _id: 'catB', Datasets: 237, Average: 27.6677358649789 },
  { _id: 'cat3', Datasets: 15, Average: 25.567020000000001 },
  { _id: 'catD', Datasets: 51, Average: 24.998184313725492 }
]

const winter = [
  { _id: 'cat1', Datasets: 496, Average: 17.586688911290324 },
  { _id: 'catB', Datasets: 237, Average: 17.6677358649789 },
  { _id: 'cat3', Datasets: 15, Average: 15.567020000000001 },
  { _id: 'catD', Datasets: 51, Average: 14.998184313725492 }
]

The shorthanded category strings should be 'translated' to the correct category label and the float values should be pushed to the season field.

const target = {
  cat1: { label: 'Cars', index: 0 },
  catB: { label: 'Planes', index: 1 },
  cat3: { label: 'People', index: 2 },
  catD: { label: 'Vegetables', index: 3 }
}

So the result for the example data should be:

{
  labels: [
    'Cars',
    'Planes',
    'People',
    'Vegetables'
  ],
  season: {
    summer: [
      27.586688911290324,
      24.998184313725492,
      27.6677358649789,
      25.567020000000001
    ]
    winter: [
      17.586688911290324,
      14.998184313725492,
      17.6677358649789,
      15.567020000000001
    ]
  },
  title: 'Just a title string'
}

My attempt looks like this, but it handles only one data array:

const result = data.reduce((r, { _id, Average }) => {
    r.labels[target[_id].index] = target[_id].label
    r.season.winter[target[_id].index] = Average
    return r
  }, { labels: [], season: { winter: [] }, title: 'Just a title string' })
3
  • the expected output is alittle vague, based on your inputs; may you explain it further Commented Mar 1, 2020 at 18:46
  • @Mhmdrz_A Labels field should get the category label which is stored in target. Average values of each array should get to winter/summer and the title is just any string. Commented Mar 1, 2020 at 18:48
  • Reduce provides an index if you want to use it (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ) Commented Mar 1, 2020 at 18:48

2 Answers 2

2

You could take an object with winter and summer as short hand properties and iterate the entries from it.

This approach takes the labels first to prevent for every object a new setting.

const
    getLabels = target => Object.values(target).reduce((r, { label, index }) => {
        r[index] = label;
        return r;
    }, []),
    summer = [{ _id: 'cat1', Datasets: 496, Average: 27.586688911290324 }, { _id: 'catB', Datasets: 237, Average: 27.6677358649789 }, { _id: 'cat3', Datasets: 15, Average: 25.567020000000001 }, { _id: 'catD', Datasets: 51, Average:  4.998184313725492 }],
    winter = [{ _id: 'cat1', Datasets: 496, Average: 17.586688911290324 }, { _id: 'catB', Datasets: 237, Average: 17.6677358649789 }, { _id: 'cat3', Datasets: 15, Average: 15.567020000000001 }, { _id: 'catD', Datasets: 51, Average: 14.998184313725492 }],
    target = { cat1: { label: 'Cars', index: 0 }, catB: { label: 'Planes', index: 1 }, cat3: { label: 'People', index: 2 }, catD: { label: 'Vegetables', index: 3 } },
    result = Object
        .entries({ summer, winter })
        .reduce((r, [key, data]) => {
            r.season[key] = [];
            data.forEach(({ _id, Average }) => r.season[key][target[_id].index] = Average);
            return r;
        }, { labels: getLabels(target), season: { }, title: 'Just a title string' });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Why is in the last line season: { winter: [] } without summer? I do not understand this part.
no need for this. it is set for any key of the object here: r.season[key] = [];
0
const summer = [
  { _id: 'cat1', Datasets: 496, Average: 27.586688911290324 },
  { _id: 'catB', Datasets: 237, Average: 27.6677358649789 },
  { _id: 'cat3', Datasets: 15, Average: 25.567020000000001 },
  { _id: 'catD', Datasets: 51, Average: 24.998184313725492 }
]

const winter = [
  { _id: 'cat1', Datasets: 496, Average: 17.586688911290324 },
  { _id: 'catB', Datasets: 237, Average: 17.6677358649789 },
  { _id: 'cat3', Datasets: 15, Average: 15.567020000000001 },
  { _id: 'catD', Datasets: 51, Average: 14.998184313725492 }
]

const target = {
  cat1: { label: 'Cars', index: 0 },
  catB: { label: 'Planes', index: 1 },
  cat3: { label: 'People', index: 2 },
  catD: { label: 'Vegetables', index: 3 }
}



let desiredObj = {
  labels: [],
  season: {
    summer: [],
    winter: []
  },
  title: 'Just a title string'
}


Object.keys(target).forEach(targetKey => {

  const targetLabel = target[targetKey].label
  const targetIndex = target[targetKey].index

  const summerObjFoundByTargetKey = summer.find(obj => obj['_id'] === targetKey)

  const winterObjFoundByTargetKey = winter.find(obj => obj['_id'] === targetKey)

  desiredObj.labels.push(targetLabel)

  if(summerObjFoundByTargetKey) {
    desiredObj.season.summer[targetIndex] = summerObjFoundByTargetKey.Average
  }

  if(winterObjFoundByTargetKey) {
    desiredObj.season.winter[targetIndex] = winterObjFoundByTargetKey.Average
  }

})


console.log('desiredObj', desiredObj)

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.