Select a specific JSON array with Javascript

Asked

Viewed 43 times

-1

I created a script with Axios to resume a call, then the result:

[
  {
    FieldId: 8095,
    FieldName: 'Nome1',
    FieldValueIds: [ 12059 ],
    FieldValues: [ '9.000' ],
    IsFilter: true,
    FieldGroupId: 99,
    FieldGroupName: 'Informações'
  },
  {
    FieldId: 8108,
    FieldName: 'Nome2',
    FieldValueIds: [ 2994 ],
    FieldValues: [ 'Frio' ],
    IsFilter: true,
    FieldGroupId: 99,
    FieldGroupName: 'Informações'
  },
  {
    FieldId: 28079,
    FieldName: 'nome3',
    FieldValueIds: [],
    FieldValues: [ '00000' ],
    IsFilter: false,
    FieldGroupId: 1,
    FieldGroupName: 'Categoria'
  }
] 

I’m just using console.log(response.data.ProdSpecification) to bring the result, but need to bring a unique result.

For example the FieldName: 'nome3' and FieldGroupName: 'Categoria' of FieldId: 28079.

2 answers

1

Use the method find and create an expression that checks the item that satisfies the search as described in your question, example:

const data = [{
    FieldId: 8095,
    FieldName: 'Nome1',
    FieldValueIds: [12059],
    FieldValues: ['9.000'],
    IsFilter: true,
    FieldGroupId: 99,
    FieldGroupName: 'Informações'
  },
  {
    FieldId: 8108,
    FieldName: 'Nome2',
    FieldValueIds: [2994],
    FieldValues: ['Frio'],
    IsFilter: true,
    FieldGroupId: 99,
    FieldGroupName: 'Informações'
  },
  {
    FieldId: 28079,
    FieldName: 'nome3',
    FieldValueIds: [],
    FieldValues: ['00000'],
    IsFilter: false,
    FieldGroupId: 1,
    FieldGroupName: 'Categoria'
  }
]

const item = data.find(x => 
    x.FieldName === 'nome3' &&  
    x.FieldGroupName === 'Categoria'
);

console.log(item);

-1

Browser other questions tagged

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