-3

I need help achieving the following task: I have an object that is structured like this

SOMEKEY: {
   "key1": val1,
   "key2": val2
}

and an array that holds string values like this

[stringA, stringB, stringC]

What I want, is an array returned by the following criteria.

return all key value pairs from the object, where the values match either one of the string arrays elements.

The result should be sorted by the values in an descending order.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user16780074
  • 429
  • 2
  • 7
  • 20
  • 1
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Aug 29 '21 at 13:39
  • 1
    Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See [ask] and [mcve] – charlietfl Aug 29 '21 at 13:42
  • An object in sorted order ?!. I think you need an array of objects instead – sojin Aug 29 '21 at 13:45
  • There is no attempt here; no sign of any effort. *"I need help"* is not a suitable question for Stack Overflow. – trincot Aug 29 '21 at 13:50
  • I am able to get the result done using filter method when I have an Array of Objects instead of an object with named keys. But I need the solution for the object structure as described initially. I have tried several approaches with Object.keys and Object.values which are not working. I probably have to combine Object.keys with an array filter function, but I just dont get it to work, or dont know how to nest all this. – user16780074 Aug 29 '21 at 13:51
  • solved. I found the Object.entries() function which I could use to get the keys to an array and use filter function with includes on the array from there. Thanks – user16780074 Aug 29 '21 at 13:54
  • in case needed this is how I solved it let temp = Object.entries(this.objectA[index]).filter(e => this.arrayB.includes(e[0])); – user16780074 Aug 29 '21 at 14:00

3 Answers3

0

You could filter() the objects entries and create a new object from those entries if the value is included in the values array.

const key = {
  "key1": "val1",
  "key2": "val2",
  "key4": "val4"
}

const values = ["val1", "val4"];

const result = Object.fromEntries(Object.entries(key).filter(([k, v]) => {
  return values.includes(v);
}));

console.log(result);
axtck
  • 3,707
  • 2
  • 10
  • 26
0

you can simply use foreach function on the object and check which values are in the string array and add them to new temp array like that:

var obj = {
    "key1": 1,
    "key2": 20,
    "key3": 3
}

var filterArray = [10, 2, 1, 3];

var tempArray = [];

Object.keys(obj).forEach((key, index)=>{
    if(filterArray.includes(obj[key]))
    {
       tempArray.push({[key]: obj[key]});
    }
});

console.log(tempArray);

// returns [ { key1: 1 }, { key3: 3 } ]
Ori
  • 16
  • 3
0

This code should work :

var values = ['a', 'b', 'y'];
var obj = {
  'k1': 'a',
  'k2': 'b',
  'k3': 'c'
}
let result = values.reduce((acc, val) => {
  for (key in obj) {
    if (obj.hasOwnProperty(key) && obj[key] == val) {
      acc.push({'key': key, 'value': val});
    }
  }
  return acc;
}, []).sort((a, b) => (a.value < b.value) ? 1 : -1);
console.log(result);
astroide
  • 807
  • 4
  • 16