ELIP
ELIP

Reputation: 311

How to check element through Loop method in JSON Array

I'm new in JavaScript. I tried to execute this code to expect result like this

[["00:04:12","05:54:46"],["06:06:42","12:45:22"],["12:51:11","15:56:11"]]

But It doesn't work. Please help me what's wrong with my script. Thank you

<!DOCTYPE html>
<html>

<body>
    <p>Access a JSON object :</p>
    <p id="demo"></p>

    <script>
        var myObj;
        var m1 = [];
        myObj = [{
            "machine": "M-MBH-(2)",
            "time": [{
                "start": "06:24:23",
                "end": "16:45:37"
            }]
        }, {
            "machine": "M-MD2.5",
            "time": [{
                "start": "00:04:12",
                "end": "05:54:46"
            }, {
                "start": "06:06:42",
                "end": "12:45:22"
            }, {
                "start": "12:51:11",
                "end": "15:56:11"
            }]
        }];

        for (var i in myObj) {
            var obj = myObj[i].time;
            if (obj === "M-MD2.5") {
                obj.forEach(function(time) {
                    var pair = [];
                    pair.push(time.start);
                    pair.push(time.end);
                    m1.push(pair);
                });
            }
        }

        document.getElementById("demo").innerHTML = JSON.stringify(m1);
    </script>

</body>

</html>

Upvotes: 2

Views: 61

Answers (3)

Ever Dev
Ever Dev

Reputation: 2152

It's easy. Using filter, map, Object.keys

JSON.stringify(
  ...myObj
    .filter(item => item.machine === 'M-MD2.5')
    .map(item => item.time.map(item1 => Object.values(item1)))
);

Upvotes: 1

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result, change if condition to check machine as below

myObj[i].machine ==="M-MD2.5"

working code sample for reference

    <!DOCTYPE html>
            <html>
            <body>
            <p>Access a JSON object :</p>
            <p id="demo"></p>

            <script>
            var myObj;
            var m1=[];
            myObj = [{
                    "machine": "M-MBH-(2)",
                    "time": [
                        {
                            "start": "06:24:23",
                            "end": "16:45:37"
                        }
                    ]
                },
                {
                    "machine": "M-MD2.5",
                    "time": [
                        {
                            "start": "00:04:12",
                            "end": "05:54:46"
                        },
                        {
                            "start": "06:06:42",
                            "end": "12:45:22"
                        },
                        {
                            "start": "12:51:11",
                            "end": "15:56:11"
                        }
                    ]
                }];

                for(var i in myObj)
                {
               var obj = myObj[i].time;
               if(myObj[i].machine ==="M-MD2.5"){
            obj.forEach(function(time) {                    
                var pair=[];
                pair.push(time.start);
                pair.push(time.end);
                m1.push(pair);
            });
                 }
              }

            document.getElementById("demo").innerHTML = JSON.stringify(m1);
            </script>

            </body>
            </html>

Issue: Value of obj variable is time and checking with machine name will always return false

Upvotes: 1

mickl
mickl

Reputation: 49975

You can use .find() to get matching element and then .map() to build new array:

let  myObj = [{
        "machine": "M-MBH-(2)",
        "time": [
            {
                "start": "06:24:23",
                "end": "16:45:37"
            }
        ]
    },
    {
        "machine": "M-MD2.5",
        "time": [
            {
                "start": "00:04:12",
                "end": "05:54:46"
            },
            {
                "start": "06:06:42",
                "end": "12:45:22"
            },
            {
                "start": "12:51:11",
                "end": "15:56:11"
            }
        ]
    }];

let result = myObj.find(x => x.machine === "M-MD2.5").time.map(({start,end}) => [start,end]);

console.log(result);

Upvotes: 1

Related Questions