Wally
Wally

Reputation: 785

map / filter object values if iteration's key is in Array

I need to filter an Objects value to a new array && only have that value pass to the array if its key is found in an Array // I'm pretty sure there will be a way to do this, I'm just struggling to apply it to my current model.

The example below of what I've been trying to work

JSON file holds an array of objects -

I have filtered the selected object and stored its keys in a separate array (minus some I do not need):

let labelValues = () => {
                // Filter the JSON data to remove not wanted
                // This is explicit so will need to be written to be dynamic
                // Data also is apearing out of context
                return Object.keys(data[0]).filter((e)=>{
                    if(e == 'COMPANY' || e == 'HEADQUARTERS' || e == 'TOTAL'){
                        return false
                    }
                    return true
                })
            }

This leaves me with:

let X = ["JANUARY", "FEBUARY", "APRIL", "MARCH", "FEBUARY_1", "MAY"]

I now need to create a new array - by iterating over an object and only having the value pass to the array if, the objects KEY is in the above array X

Example of data structure of object:

let obj = {
APRIL: 35
COMPANY: "Walmart"
FEBUARY: 34
FEBUARY_1: 9
HEADQUARTERS: "Bentonville, AR"
JANUARY: 22.6
MARCH: 23.4
MAY: 21.1
TOTAL: 145.1
}

the desired array would be:

[35,34,9,22.6,23.4,21.1]

Thanks in advance - Wally

Upvotes: 0

Views: 382

Answers (3)

Wally
Wally

Reputation: 785

Ok so I figured this out.... and I now feel somewhat silly that the answer is so simple....

basically -

create a new array i.e.

let array = []

then with the values of the keys run a forEach on that pushing the data into the new array i.e.

values.forEach(value => {
    array.push(data[0][value])
})

BTW the data[0] will be dynamic - just using it as reference for the moment

SILLY SILLY ME!

thanks for your help

Upvotes: 0

Slai
Slai

Reputation: 22876

for loop can be used to go over the object keys and add the values :

var obj = { APRIL: 35, COMPANY: "Walmart", FEBUARY: 34, FEBUARY_1: 9,
  HEADQUARTERS: "Bentonville, AR", JANUARY: 22.6, MARCH: 23.4, MAY: 21.1, TOTAL: 145.1 }

var arr = []

for (var key in obj)
  if (key !== 'COMPANY' && key !== 'HEADQUARTERS' && key !== 'TOTAL')
    arr.push(obj[key])
    
console.log( JSON.stringify( arr ) )
console.log( '[35,34,9,22.6,23.4,21.1]' )

It can also be done during parsing :

var json = '{"APRIL":35,"COMPANY":"Walmart","FEBUARY":34,"FEBUARY_1":9,"HEADQUARTERS":"Bentonville, AR","JANUARY":22.6,"MARCH":23.4,"MAY":21.1,"TOTAL":145.1}'

var arr = []
JSON.parse(json, (k, v) => !isNaN(v) && k !== 'TOTAL' && arr.push(v))
    
console.log( JSON.stringify( arr ) )

Upvotes: 1

user2023540
user2023540

Reputation:

So in this case:

let values = obj.map((item)=>{
// if items key is not found do not pass it to the new array
if(item.Object.key != X){
return false
}
return item
})

Would leave you with an array of true/false values.

Here is a short code snippet I wrote that should do what you want it to do:

const obj = {
  APRIL: 35,
  COMPANY: 'Walmart',
  FEBUARY: 34,
  FEBUARY_1: 9,
  HEADQUARTERS: 'Bentonville, AR',
  JANUARY: 22.6,
  MARCH: 23.4,
  MAY: 21.1,
  TOTAL: 145.1,
};

const notInclude = ['COMPANY', 'HEADQUARTERS', 'TOTAL'];

let labelValues = () => {
  return Object.keys(obj).filter((val) => !notInclude.includes(val));
};

const data = labelValues();
const result = data.map((val) => obj[val]);

console.log(data);
console.log(result);

That should do the trick, but let me know if that doesn't make sense!

Upvotes: 1

Related Questions