0

sorry guys but i have a dummy question.. i need to iterate objects from a json response and obtain only those how meet some conditions.. the response is like this:

  result = [
      {
        "type": "EVENT",
        "id": "001",
        "tags": [
            {
            "id": "98765",
            "name": "home"
            }
        ]
      },
      {
        "type": "EVENT",
        "id": "002",
        "tags": [
            {
            "id": "7654",
            "name": "contact"
            }
        ]
      },
      {
        "type": "EVENT",
        "id": "003",
        "tags": []
      }
    ]

I need to use only those whose type is 'event' and name properties in tags be home.

I tried map and filter, but I do not get the desired result

const eventType = result.filter(type => type.type == 'EVENT')
 const nameFilter = 
    eventType.map(item => item.tags)
    .filter(sub => sub.length) // remove empty []
    .map(subarray => subarray.map(element =>  element.name )
    .filter(element => element == 'home')); 

result:

 [
  ['home'], // dosen t work for me, because need all the object
  [],
  []
 ]

3 Answers 3

3

You can use filter to loop thru the array. Use some to check if at least one element in the tags array has a property name with value of "home":

result.filter(o => o.type === "EVENT" && o.tags.some(s => s.name === "home"));

Demo:

let result = [{"type":"EVENT","id":"001","tags":[{"id":"98765","name":"home"}]},{"type":"EVENT","id":"002","tags":[{"id":"7654","name":"contact"}]},{"type":"EVENT","id":"003","tags":[]}];

let filtered = result.filter(o => o.type === "EVENT" && o.tags.some(s => s.name === "home"));

console.log( filtered );

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

Comments

0

You only need filter() and use find() or some() to check exist name in tags.

var filter = result.filter(c=> c.type == "EVENT" && c.tags.find(d=>d.name == 'home'));
var filter = result.filter(c=> c.type == "EVENT" && c.tags.some(d=>d.name == 'home'));

let result=  [
      {
        "type": "EVENT",
        "id": "001",
        "tags": [
            {
            "id": "98765",
            "name": "home"
            }
        ]
      },
      {
        "type": "EVENT",
        "id": "002",
        "tags": [
            {
            "id": "7654",
            "name": "contact"
            }
        ]
      },
      {
        "type": "EVENT",
        "id": "003",
        "tags": []
      }
    ]
    
    var filter = result.filter(c=> c.type == "EVENT" && c.tags.find(d=>d.name == 'home'));
    
    console.log(filter);

Comments

-1

if you simply want to select elements from the root array then filter is the appropriate way to do. if you want to also transform the result (say, select the TAG object), you may use flatMap

result.flatMap( e => (e.type == 'EVENT' && e.tags.some(t => t.name==='home')) ? e.tags : [] )

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.