0

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

1
  • No - sorry that was a typo on my part - I will update Commented Sep 3, 2020 at 17:24

3 Answers 3

1

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!

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

4 Comments

Hi Graham - Thats amazing for your input however it didn't seem to work as intended - I have edited my question to be more precise on the result I am looking for. Thanks again for your input (also let me know if I'm being thick and the above works in that context) - thanks again - W
Im looking at this and now thinking because it is a OBJ i would need to create an array first and then push() the value into the array if the key is found?
@Wally Take a look at that and let me know if that makes any more sense!
Yeah that solves the issue in a way smoother way than mine :P thank you for your help :)
1

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 ) )

Comments

0

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

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.