Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I filter a double array and return an object in React?

I have an array object in a variable called maintemp.

const maintemp = [
[
    {
    equipId: 1,
    name: '1메인 온도 센서',
    sensors: [
        {
        tagId: 11,
        name: '온도',
        value: 28.8,
        tagClass: {
            bitCalcYn: 'N',
            code: 'INNERTEMP',
            encoding: 'ONEDECIMALPOINT',
            isDigital: 'N',
        },
        },
    ],
    },
    {
    equipId: 2,
    name: '1메인 습도 센서',
    sensors: [
        {
        tagId: 22,
        name: '습도',
        value: 50.3,
        tagClass: {
            bitCalcYn: 'N',
            code: 'INNERAHUM',
            encoding: 'ONEDECIMALPOINT',
            isDigital: 'N',
            name: '실내 습도',
        },
        },
    ],
    },
],

[
    {
    equipId: 3,
    name: '2메인 온도 센서',
    sensors: [
        {
        tagId: 11,
        name: '온도',
        value: 28.8,
        tagClass: {
            bitCalcYn: 'N',
            code: 'INNERTEMP',
            encoding: 'ONEDECIMALPOINT',
            isDigital: 'N',
        },
        },
    ],
    },
    {
    equipId: 4,
    name: '2메인 습도 센서',
    sensors: [
        {
        tagId: 22,
        name: '습도',
        value: 50.3,
        tagClass: {
            bitCalcYn: 'N',
            code: 'INNERAHUM',
            encoding: 'ONEDECIMALPOINT',
            isDigital: 'N',
            name: '실내 습도',
        },
        },
    ],
    },
],
];

At this time, I want to put only the object whose tagClass.code is ‘INNERTEMP’ into the variable fill by using the map function and the filter function.

However, when I run my code, the entire array is not filtered and the entire array is stored in the fill variable. How do I fix the code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is my code

const fill = maintemp.filter((v) =>
  v.filter((hi) => hi.sensors[0].tagClass.code === "INNERTEMP'")
);

expected answer

const fill = [
    {
        equipId: 1,
        name: '1메인 온도 센서',
        sensors: [
        {
            tagId: 11,
            name: '온도',
            value: 28.8,
            tagClass: {
            bitCalcYn: 'N',
            code: 'INNERTEMP',
            encoding: 'ONEDECIMALPOINT',
            isDigital: 'N',
            },
        },
        ],
    },
    {
        equipId: 3,
        name: '2메인 온도 센서',
        sensors: [
        {
            tagId: 11,
            name: '온도',
            value: 28.8,
            tagClass: {
            bitCalcYn: 'N',
            code: 'INNERTEMP',
            encoding: 'ONEDECIMALPOINT',
            isDigital: 'N',
            },
        },
        ],
    },

]

>Solution :

You can first flatten your array then filter items:

fill = maintemp.flat().filter((hi) => hi.sensors[0].tagClass.code === "INNERTEMP")
const maintemp = [[{"equipId":1,"name":"1메인 온도 센서","sensors":[{"tagId":11,"name":"온도","value":28.8,"tagClass":{"bitCalcYn":"N","code":"INNERTEMP","encoding":"ONEDECIMALPOINT","isDigital":"N"}}]},{"equipId":2,"name":"1메인 습도 센서","sensors":[{"tagId":22,"name":"습도","value":50.3,"tagClass":{"bitCalcYn":"N","code":"INNERAHUM","encoding":"ONEDECIMALPOINT","isDigital":"N","name":"실내 습도"}}]}],[{"equipId":3,"name":"2메인 온도 센서","sensors":[{"tagId":11,"name":"온도","value":28.8,"tagClass":{"bitCalcYn":"N","code":"INNERTEMP","encoding":"ONEDECIMALPOINT","isDigital":"N"}}]},{"equipId":4,"name":"2메인 습도 센서","sensors":[{"tagId":22,"name":"습도","value":50.3,"tagClass":{"bitCalcYn":"N","code":"INNERAHUM","encoding":"ONEDECIMALPOINT","isDigital":"N","name":"실내 습도"}}]}]];

const fill = maintemp.flat().filter((hi) => hi.sensors[0].tagClass.code === "INNERTEMP")

console.log(fill)
.as-console-wrapper { max-height: 100% !important; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading