0

This is my json data -

{
  "list": [
    {
      "deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
      "versions": [
        {
          "id": "764c20-a213-9235f4b553b3",
          "createdTime": 1590361208034,
          "files": [
            {
              "fileType": "VLAN"
            },
            {
              "fileType": "STARTUPCONFIG",
            }
          ],
          "startupRunningStatus": "OUT_OF_SYNC",
          "createdBy": "SCHEDULED"
        },
        {
          "id": "9bd33-a45a-ed2fefc46931",
          "createdTime": 1589972337717,
          "files": [
            {
              "fileType": "VLAN",
            },
            {
              "fileType": "STARTUPCONFIG",
            },
            {
              "fileType": "RUNNINGCONFIG",
            }
          ],
          "startupRunningStatus": "IN_SYNC",
          "createdBy": "SCHEDULED_FIRST_TIME"
        }
      ]
    }
  ]
}

I want to filter data, where "fileType": "RUNNINGCONFIG", and "fileType": "STARTUPCONFIG", is present inside 'files' only return that array.

e.g. From the above json only second object will be returned.

I tried to write some Filter code but it is not working properly, guide me.

let versionsData = response.data.versions;


versionsData = versionsData.filter(
                        versions => {
                            versions.files.filter(
                                m =>  {
                                    return m.fileType === "RUNNINGCONFIG"
                                }
                            )
                        }
                    );
                return versionsData;

3 Answers 3

5

Instead of nested filter you could use Array.prototype.some

const obj = {
  "list": [
    {
      "deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
      "versions": [
        {
          "id": "764c20-a213-9235f4b553b3",
          "createdTime": 1590361208034,
          "files": [
            {
              "fileType": "VLAN"
            },
            {
              "fileType": "STARTUPCONFIG",
            }
          ],
          "startupRunningStatus": "OUT_OF_SYNC",
          "createdBy": "SCHEDULED"
        },
        {
          "id": "9bd33-a45a-ed2fefc46931",
          "createdTime": 1589972337717,
          "files": [
            {
              "fileType": "VLAN",
            },
            {
              "fileType": "STARTUPCONFIG",
            },
            {
              "fileType": "RUNNINGCONFIG",
            }
          ],
          "startupRunningStatus": "IN_SYNC",
          "createdBy": "SCHEDULED_FIRST_TIME"
        }
      ]
    }
  ]
}
let versionsData = obj.list[0].versions;


versionsData = versionsData.filter(
    versions => 
        versions.files.some(
            m =>  m.fileType === "RUNNINGCONFIG"
        ) && versions.files.some(
            m => m.fileType== 'STARTUPCONFIG'
        )

);
console.log(versionsData);

With Array.prototype.filter

const obj = {
  "list": [
    {
      "deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
      "versions": [
        {
          "id": "764c20-a213-9235f4b553b3",
          "createdTime": 1590361208034,
          "files": [
            {
              "fileType": "VLAN"
            },
            {
              "fileType": "STARTUPCONFIG",
            }
          ],
          "startupRunningStatus": "OUT_OF_SYNC",
          "createdBy": "SCHEDULED"
        },
        {
          "id": "9bd33-a45a-ed2fefc46931",
          "createdTime": 1589972337717,
          "files": [
            {
              "fileType": "VLAN",
            },
            {
              "fileType": "STARTUPCONFIG",
            },
            {
              "fileType": "RUNNINGCONFIG",
            }
          ],
          "startupRunningStatus": "IN_SYNC",
          "createdBy": "SCHEDULED_FIRST_TIME"
        }
      ]
    }
  ]
}
let versionsData = obj.list[0].versions;


versionsData = versionsData.filter( versions => {        return versions.files.filter( m => { 
     return m.fileType === "RUNNINGCONFIG" || m.fileType === "STARTUPCONFIG" }).length > 1 }
);
console.log(versionsData);

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

13 Comments

after running your code it still show 2 objects, it should be one right.
Do you need both conditions satisfied?
@ShubhamKhatri, yes both condition should be satisfied, I replaced || with && but it's not working.
Some returns you true if a condition is satisfied by atleast one element in the array. However filter will loop on the entire array and returns you elements that satisfy the condition. It could have worked with filter too but you would need to change its implementation a bit
With filter you would need to write versionsData.filter( versions => { versions.files.filter( m => { return m.fileType === "RUNNINGCONFIG" || m.fileType === "STARTUPCONFIG" } ).length > 0 } );
|
1

A way with filter inside of filter.

let data = {
  "list": [
    {
      "deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
      "versions": [
        {
          "id": "764c20-a213-9235f4b553b3",
          "createdTime": 1590361208034,
          "files": [
            {
              "fileType": "VLAN"
            },
            {
              "fileType": "STARTUPCONFIG",
            }
          ],
          "startupRunningStatus": "OUT_OF_SYNC",
          "createdBy": "SCHEDULED"
        },
        {
          "id": "9bd33-a45a-ed2fefc46931",
          "createdTime": 1589972337717,
          "files": [
            {
              "fileType": "VLAN",
            },
            {
              "fileType": "STARTUPCONFIG",
            },
            {
              "fileType": "RUNNINGCONFIG",
            }
          ],
          "startupRunningStatus": "IN_SYNC",
          "createdBy": "SCHEDULED_FIRST_TIME"
        }
      ]
    }
  ]
};


let versionsData = /*response.*/data.list[0].versions;

versionsData = versionsData.filter(versions => {
  return versions.files.filter(m => {
    return m.fileType == 'RUNNINGCONFIG' || m.fileType == 'STARTUPCONFIG';
  }).length == 2;
});

console.log(versionsData);

Comments

0

const data = {
  "list": [
    {
      "deviceId": "2a-d539-4031-9bfc-4a42f2f765cf",
      "versions": [
        {
          "id": "764c20-a213-9235f4b553b3",
          "createdTime": 1590361208034,
          "files": [
            {
              "fileType": "VLAN"
            },
            {
              "fileType": "STARTUPCONFIG",
            }
          ],
          "startupRunningStatus": "OUT_OF_SYNC",
          "createdBy": "SCHEDULED"
        },
        {
          "id": "9bd33-a45a-ed2fefc46931",
          "createdTime": 1589972337717,
          "files": [
            {
              "fileType": "VLAN",
            },
            {
              "fileType": "STARTUPCONFIG",
            },
            {
              "fileType": "RUNNINGCONFIG",
            }
          ],
          "startupRunningStatus": "IN_SYNC",
          "createdBy": "SCHEDULED_FIRST_TIME"
        }
      ]
    }
  ]
}

const filterData = () => {
  return data.list[0].versions.reduce((output, version) => {
    const fileTypes = version.files.map(f => f.fileType);

    return fileTypes.includes('STARTUPCONFIG') && fileTypes.includes('RUNNINGCONFIG') ? 
    [...output, version] : output
  }, [])
}

console.log(filterData());

2 Comments

You forget to add console, so that we can run and check.
added the log. please try now.

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.