skip to Main Content

I am trying to filter this JSON object by the keys:

The data looks like this

key1: value1
key2: value2
key3: value3
key4: value4
key5: value5
key6: value6
key7: value7
key8: value8
key9: value9

Currently what I am doing right now is renaming the keys to fit that strucure and then try to filter it out.

    var reassaignedKeys;

    props.data.map((entries, valuesIndex) => {
      reassaignedKeys =  Object.assign({}, {Owner: entries[0], ApplicationId: entries[1], ApplicationName: entries[2], Status: entries[3], DeploymentStatus: entries[4], Classification: entries[5], PersonalBindle: entries[6], PerviouslyCertified: entries[7], Goal: entries[8]})
  
    })
    console.log(reassaignedKeys)   

This code above is what I am using to rename the keys, I am now confused on how to filter out the key and value using the key.

I know I can use

Object.keys.filter

But I am not getting the results I want is it keeps returning the same array back to me

2

Answers


  1. I don’t quite understand what you wan’t, but if you want get keys or key-values pairs, and assuming that your JSON is correct
    like this.

    [{"key1":"value1"},
    {"key2":"value2"},
    {"key3":"value3"},
    {"key4":"value4"},
    {"key5":"value5"},
    {"key6":"value6"},
    {"key7":"value7"},
    {"key8":"value8"},
    {"key9":"value9"}]
    

    Than you can do JSON.parse to parse your JSON to JS object, and use

    To get keys

    Object.keys(converted_obj_from_json);
    

    To get key-value pairs

    Object.entries(converted_obj_from_json);
    
    Login or Signup to reply.
  2. You can make something like this :

    const filteredJson = Object.entries(originalJson).reduce((acc,[key,value])=>{
      const isValidKey = key !== 'A' // for exemple
      if(!isValidKey) return acc
      return {
             ...acc,
            [key]:value}
    },{})
    

    You can also use lodash pickBy or omitBy functions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search