2

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>
1
  • I used to have the problem . make sure myobj is an object json not a string . Commented Jan 13, 2020 at 3:55

3 Answers 3

1

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);

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

Comments

1

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

Comments

1

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)))
);

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.