0

I've got two multidimensional arrays:

const defaultData = [
  ["ad", 0],
  ["ae", 0],
  ["af", 0]
]

const data = [
  ["az", 20],
  ["ad", 50]
]

The desired result is:

const expected = [
  ["ad", 50],
  ["ae", 0],
  ["af", 0]
]

Here az is ignored because its not valid because its not in the defaultData. ad is valid and therefore the 0 is overwritten with the value of 50.

How would I best produce this result. Thanks?

Please assume neither array is sorted. The expected result need not be sorted either.

Either vanilla JS or Lodash is fine.

5 Answers 5

2

Convert the data to an object (dataByKey) with _.fromPairs(). Map the defaultData, and check if the "key" (index 0) exists in dataByKey. If it is, create a new array with the value from dataByKey. If not return the original array.

const defaultData = [
  ["ad", 0],
  ["ae", 0],
  ["af", 0]
]

const data = [
  ["az", 20],
  ["ad", 50]
]

const dataByKey = _.fromPairs(data)

const result = defaultData.map(o => o[0] in dataByKey ? [o[0], dataByKey[o[0]]] : o)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

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

Comments

1

Use map to iterate over the defaultData, and find to match each iterated array against data:

const defaultData = [['ad', 0],['ae', 0],['af', 0]];
const data = [['az', 20],['ad', 50]];

const expected = defaultData.map(arr => {

  // Destructure the first element from `arr`
  // and assign it to `code`
  const [ code ] = arr;

  // `find` the array in `data` where the first element
  // matches `code`
  const found = data.find(el => el[0] === code);

  // If `found` is undefined return the whole array
  // otherwise return a new array with the code and the value
  // from the found array
  return found ? [code, found[1]] : arr;
});

console.log(expected);

Comments

1

You could first create one object from data and then use map method on defaultData to create new array and get value from data by key.

const defaultData = [
  ["ad", 0],
  ["ae", 0],
  ["af", 0]
]

const data = [
  ["az", 20],
  ["ad", 50]
].reduce((r, [key, value]) => {
  r[key] = (r[key] || 0) + value;
  return r;
}, {})

const expected = defaultData.map(([key, value]) => {
  return [key, key in data ? data[key] : value] 
})

console.log(expected)

Comments

1

Try this out, I am using map() to create a new Array and find() to find elements from the second array.

I compare the first index item of each sub array from the default array, if any of the items from second array has the same element in its first index then I return it, if none matches, nothing is returned.

If I have a matched item then I create a new array using its elements. If I don't have a matched item I create a new using the default elements of the default item.

const defaultData = [
  ["ad", 0],
  ["ae", 0],
  ["af", 0]
]

const data = [
  ["az", 20],
  ["ad", 50]
]

let newData = defaultData.map(eachDefault => {
  let found = data.find(eachData => {
    if (eachData[0] == eachDefault[0]) {
      return true
    }
  })
  if (found) {
    return [...found]
  }
  return [...eachDefault]
})

console.log(newData)

1 Comment

Answers saying "Try this out:" are not appreciated. You should explain your code.
1

You can use map/reduce like this:

const defaultData = [
  ["ad", 0],
  ["ae", 0],
  ["af", 0]
]

const data = [
  ["az", 20],
  ["ad", 50]
]

const result = defaultData.map(arr => data.reduce(([dKey, dVal], [key, val]) => [ dKey, dKey === key ? val : dVal ], arr));

console.log(result);

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.