Narik
Narik

Reputation: 166

Extract items from a json file with values in an array

I am trying to handle JSON arrays to extract key value pair from the keys available from user selection.

This is not real time json example...It is just a sample

Example of JSON

var personnel = [
  {
    id: 5,
    name: "Luke Skywalker",
    pilotingScore: 98,
    shootingScore: 56,
    isForceUser: true,
  },
  {
    id: 82,
    name: "Sabine Wren",
    pilotingScore: 73,
    shootingScore: 99,
    isForceUser: false,
    skills:{
      'skill1':'vision',
      'skill2':'strength'  
    }
  },
  {
    id: 22,
    name: "Zeb Orellios",
    pilotingScore: 20,
    shootingScore: 59,
    isForceUser: false,
  },
  {
    id: 15,
    name: "Ezra Bridger",
    pilotingScore: 43,
    shootingScore: 67,
    isForceUser: true,
    skills:{
      'skill1':'vision',
      'skill2':'strength'
    }
  },
  {
    id: 11,
    name: "Caleb Dume",
    pilotingScore: 71,
    shootingScore: 85,
    isForceUser: true,
  },
];




      sample_arr = [id,name,skills.skill1];

      let op = personnel.map(md => {
             return { id: md.id,name:md.name,skills{skill1:md.skills.skill1}};
       });

         console.log(JSON.stringify(op,null,2))

I wanted to get key value pair like below.

[
  {
    "id": 5,
    "name": "Luke Skywalker"
  },
  {
    "id": 82,
    "name": "Sabine Wren",
     "skills":{
         "skill1": 'vision'
       }
  },
  {
    "id": 22,
    "name": "Zeb Orellios"
  },
  {
    "id": 15,
    "name": "Ezra Bridger"
  },
  {
    "id": 11,
    "name": "Caleb Dume"
  }
]

I now updated the my problem statement.

Requirement:

Extract all JSON values selected by the user to a new array. This will save more time as the json is 700MB all the way and it is time consuming to handle on every request

Upvotes: 0

Views: 86

Answers (4)

Akrion
Akrion

Reputation: 18515

You can create a function which extracts the props from an object based on a passed array of keys:

var data = [ { id: 5, name: "Luke Skywalker", pilotingScore: 98, shootingScore: 56, isForceUser: true, }, { id: 82, name: "Sabine Wren", pilotingScore: 73, shootingScore: 99, isForceUser: false, skills:{ 'skill1':'vision', 'skill2':'strength' } }, { id: 22, name: "Zeb Orellios", pilotingScore: 20, shootingScore: 59, isForceUser: false, }, { id: 15, name: "Ezra Bridger", pilotingScore: 43, shootingScore: 67, isForceUser: true, skills:{ 'skill1':'vision', 'skill2':'strength' } }, { id: 11, name: "Caleb Dume", pilotingScore: 71, shootingScore: 85, isForceUser: true, }, ];

let pick = (obj, fields) => Object.keys(obj)
  .reduce((r,c) => (fields.includes(c) ? r[c] = obj[c] : null, r), {})

let result = data.map(x => pick(x, ['id', 'name', 'skills']))

console.log(result)

Then all you need is to loop though via Array.map to pick from all objects.

Upvotes: 0

Scott Sauyet
Scott Sauyet

Reputation: 50797

Here's a simple function to do this:

const project = (keys) => (xs) =>
  xs .map (x => keys .reduce ( (a, k) => ({...a, [k]: x[k]}), {} ))

var personnel = [{id:5,name:"Luke Skywalker",pilotingScore:98,shootingScore:56,isForceUser:true},{id:82,name:"Sabine Wren",pilotingScore:73,shootingScore:99,isForceUser:false,skills:{skill1:"vision",skill2:"strength"}},{id:22,name:"Zeb Orellios",pilotingScore:20,shootingScore:59,isForceUser:false},{id:15,name:"Ezra Bridger",pilotingScore:43,shootingScore:67,isForceUser:true,skills:{skill1:"vision",skill2:"strength"}},{id:11,name:"Caleb Dume",pilotingScore:71,shootingScore:85,isForceUser:true}];

console .log (
  project (['id', 'name']) (personnel)
)

The name project is from Codd's early papers on relational databases; it's similar in feel to SQL's select statement.

Update

The answer from KellyKapoor has one feature the above lacks: it only includes the property name if the data has it (so no skills: undefined.)

It's not clear which behavior the OP is looking for, but this minor modification offers that feature

const project2 = (keys) => (xs) =>
  xs .map (x => keys .reduce ((a, k) => ({...a, ...(k in x ? {[k]: x[k]} : {}) }), {} ))

var personnel = [{id:5,name:"Luke Skywalker",pilotingScore:98,shootingScore:56,isForceUser:true},{id:82,name:"Sabine Wren",pilotingScore:73,shootingScore:99,isForceUser:false,skills:{skill1:"vision",skill2:"strength"}},{id:22,name:"Zeb Orellios",pilotingScore:20,shootingScore:59,isForceUser:false},{id:15,name:"Ezra Bridger",pilotingScore:43,shootingScore:67,isForceUser:true,skills:{skill1:"vision",skill2:"strength"}},{id:11,name:"Caleb Dume",pilotingScore:71,shootingScore:85,isForceUser:true}];

console .log (
  project2 (['id', 'name', 'skills']) (personnel)
)

Upvotes: 1

Mauricio Carlezzo
Mauricio Carlezzo

Reputation: 140

Whats the problem with this?

let op = personnel.map(md => {
   return { id: md.id,name:md.name};
});

Upvotes: 0

KellyKapoor
KellyKapoor

Reputation: 71

You have the user selections stored in the array? If so, you could do something like:

let sample_arr = ['id', 'name']

let op = personnel.map(md => {
    let user = {}
    sample_arr.forEach(val => {
        if (md[val]) {
            user[val] = md[val]
        }
    })
    return user
})

Upvotes: 1

Related Questions