Pass the value of a key when creating or after creating the array

Asked

Viewed 33 times

1

I have the following JSON file:

const animais = {"types":[
  {
    "id":100196,
    "animal":"dog",
    "type":"adult",
    "tags":"AA 96 87"
  },{
    "objid":100578,
    "animal":"cat",
    "type":"old",
    "tags":"T2 96 AB"
  },{
    "objid":100643,
    "animal":"parrot",
    "type":"young",
    "tags":"MAN L5 65"
  },{
    "objid":101165,
    "animal":"falcon",
    "type":"teen",
    "tags":"T2 56 AA"
  },{
    "objid":101168,
    "animal":"rabbit",
    "type":"old",
    "tags":"FII R25 75"
  },{
    "objid":164713,
    "animal":"rabbit",
    "type":"teen",
    "tags":"MAN R25 75"
  },{
    "objid":101174,
    "animal":"horse",
    "type":"baby",
    "tags":"MAN R62 75"
  },{
    "objid":101177,
    "animal":"turtle",
    "type":"old",
    "tags":"MAN R4 75"
  },{
    "objid":101180,
    "animal":"tiger",
    "type":"young",
    "tags":"MAN R8 75"
  }
]}

The die was treated to return the separated animals within the same array, possessing only the animal’s name and the tags it loads.

const types = animais.types.reduce((animal, item) => {
    const val = item.animal;
    animal[val] = animal[val] || [];
    animal[val].push(item);
    return animal;
    },{}
)

threeanimals = Object.entries(types)
    .filter(([key, value]) => value.length < 3)
    .map(([key, value]) => ({ key, tags: value.map(({tags}) => tags) }))

console.log(threeanimals)

The treatise returns me an Object Array with a key, which bears the name of the animal and an array of tags, with the tags that the object had.

[Object {key: "dog", tags: ["AA 96 87"]}, Object {key: "cat", tags: ["T2 96 AB"]}, …]

What could I do besides the tags and the name of the object as key, was returned to me for example, the first snippet of the tag name as key of the tag and the objid as value? As follows:

[Object {key: "dog", tags: ["AA": "100196"]}, Object {key: "cat", tags: ["T2": "100578"]}, …]

1 answer

2


A minimal example of how to create this solution using map instead of reducer:

const animais = {
  "types": [{
    "objid": 100196,
    "animal": "dog",
    "type": "adult",
    "tags": "AA 96 87"
  }, {
    "objid": 100578,
    "animal": "cat",
    "type": "old",
    "tags": "T2 96 AB"
  }, {
    "objid": 100643,
    "animal": "parrot",
    "type": "young",
    "tags": "MAN L5 65"
  }, {
    "objid": 101165,
    "animal": "falcon",
    "type": "teen",
    "tags": "T2 56 AA"
  }, {
    "objid": 101168,
    "animal": "rabbit",
    "type": "old",
    "tags": "FII R25 75"
  }, {
    "objid": 164713,
    "animal": "rabbit",
    "type": "teen",
    "tags": "MAN R25 75"
  }, {
    "objid": 101174,
    "animal": "horse",
    "type": "baby",
    "tags": "MAN R62 75"
  }, {
    "objid": 101177,
    "animal": "turtle",
    "type": "old",
    "tags": "MAN R4 75"
  }, {
    "objid": 101180,
    "animal": "tiger",
    "type": "young",
    "tags": "MAN R8 75"
  }]
}

const types = animais.types.map((obj, index) => {      
  const tags = obj.tags.split(' ');
  return {
    key: obj.animal,
    tags: {
      [tags[0]]: obj.objid
    }
  };
}, {})

console.log(types);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.