0

i have been trying to match a certain value from the values(which is in array) of key-value pair.Like if i wanna search for id 462 ,i want to search it iteratively like first search in 2013 if not found then 2014 and so on.

i have tried Object.values(x) but its returning all the arrays for both 2 years.

x={'2013': 
   { matchId: 
      [ 
        '386',
        '387',
        '388',
        '389',
        '390',
         ] },
  '2014': 
   { matchId: 
      [
        '462',
        '463',
        '464',
        '465',
        '466',
         ] },

}

if value is found i want to insert some new key-value pair in that (year eg:-2013) like

{'2013': 
   { matchId: 
      [ 
        '386',
        '387',
        '388',
        '389',
        '390',
         ] 
      'Andrew':'23',
      'Castle':32}
}
  • 1
    Possible duplicate of [Filtering array of objects with arrays based on nested value](https://stackoverflow.com/questions/38375646/filtering-array-of-objects-with-arrays-based-on-nested-value) – Heretic Monkey Sep 17 '19 at 14:06

3 Answers3

1

Sort the keys, loop through them in order, and check if the value you want is in the array:

const keys = Object.keys(x).sort();
const testValue = '462';
let foundKey;
for (let i = 0; i < keys.length; ++i) {
  if (x[keys[i]].matchId.includes(testValue)) {
    foundKey = keys[i];
    break;
  }
}

At the end of this, foundKey will be undefined if there's no match, or the key (year) of the first matching object if found.

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
1

Iterate over the outer keys, years, and if the inner matchId array includes the search id, add it to the year object.

const data = {
  '2013': { 
    matchId: ['386', '387', '388', '389', '390']
  },
  '2014': {
    matchId: ['462', '463', '464', '465', '466']
  },
};

const addDataById = (array, searchId, data) => {
  return array && Object.values(array)
    .map(year => {
      if (year.matchId && Object.values(year.matchId).includes(searchId)) {
        return {...year, ...data};
      }
      return year;
    });
};

const newData = addDataById(data, '462', { key: 'newData' });
console.log(newData);

const newData2 = addDataById(data, '123', { key2: 'newData' });
console.log(newData2);

console.log(addDataById(undefined, '462', { key: 'newData' }));
console.log(addDataById(5, '462', { key: 'newData' }));
console.log(addDataById('array', '462', { key: 'newData' }));
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • In case of prevent unexpected error, please check the object before using `Object.values`. Do something like this:`return typeof array === 'object' && array !== null && Object.values(array)` – Herb Sep 17 '19 at 14:25
  • 1
    @Herb Thanks for the input, not really in the scope of the question though, but I did add a couple guards. An alternative could also be to throw it all in a try/catch and return a defined value if it all goes sideways. – Drew Reese Sep 17 '19 at 14:35
0

you can use hasOwnProperty() method. Use the below code to do your task

x = {
    '2013':
    {
        matchId:
            [
                '386',
                '387',
                '388',
                '389',
                '390',
            ]
    },
    '2014':
    {
        matchId:
            [
                '462',
                '463',
                '464',
                '465',
                '466',
            ]
    },

}

function findById(id) {
    for (var key in x) {
        
        if (x.hasOwnProperty(key)) {
            if (x[key]['matchId'].find(x => x == id)) { return { [key]: x[key] } }
        }
    }
}

console.log(findById('390'))
Zulqarnain Jalil
  • 1,679
  • 16
  • 26