0

i have a nested array of object and i want to convert in arraylist like this : this is my data array of object :

{
    "status": true,
    "message": "",
    "data": [{
            "pasien_docs": [{
                    "ecg": null,
                    "date": "2020-01-21T05:22:01.901Z"
                }, {
                    "ecg": 1.03,
                    "date": "2020-01-21T05:22:02.979Z"
                }, {
                    "ecg": 1.04,
                    "date": "2020-01-21T05:22:04.053Z"
                }, {
                    "ecg": 1.04,
                    "date": "2020-01-21T05:22:05.126Z"
                },
            ]
        }
    ]
}

and i want change convert to array like this :

{
    "status": true,
    "message": "",
    "data": [
        [
            "2020-01-21T05:22:01.901Z",
            null
        ],
        [
            "2020-01-21T05:22:01.901Z",
            1, 03
        ]
        [
            "2020-01-21T05:22:01.901Z",
            1.04
        ]
        [
            "2020-01-21T05:22:01.901Z",
            1.04
        ]
    ]
 }

i try using map to convert on result like this :

result = result.map((u, i) => [
            u.pasien_docs[i].date,
            u.pasien_docs[i].ecg,
        ]);

but why i only get result data of one array not four data ? help me please, thankyou..

{
    "status": true,
    "message": "",
    "data": [
        [
            "2020-01-21T05:22:01.901Z",
            null
        ]
    ]
}
3
  • u only has a single element, and i is the index of result, not u Commented May 29, 2020 at 12:23
  • Is it always only one item in data array? Commented May 29, 2020 at 12:27
  • There is no arraylist in Javascript Commented May 29, 2020 at 12:29

3 Answers 3

6

Would that work for you?

const src = {"status":true,"message":"","data":[{"pasien_docs":[{"ecg":null,"date":"2020-01-21T05:22:01.901Z"},{"ecg":1.03,"date":"2020-01-21T05:22:02.979Z"},{"ecg":1.04,"date":"2020-01-21T05:22:04.053Z"},{"ecg":1.04,"date":"2020-01-21T05:22:05.126Z"},]}]},

result = {
  ...src, 
  data: src.data[0].pasien_docs.map(Object.values)
}

console.log(result)
.as-console-wrapper{min-height:100%;}

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

4 Comments

Personal opinion, but i think it looks nicer with .map(el => Object.values(el))
It may be even nicer as .map(Object.values), but I believe it may be somewhat slower
is src the same as result?
@lfaruki : maybe you're right, besides, destructuring syntax may appear more confusing than Object.values(), thanks for your comment
2

If you dont wanna use spread operator, this can also do the trick for you

const source = {"status":true,"message":"","data":[{"pasien_docs":[{"ecg":null,"date":"2020-01-21T05:22:01.901Z"},{"ecg":1.03,"date":"2020-01-21T05:22:02.979Z"},{"ecg":1.04,"date":"2020-01-21T05:22:04.053Z"},{"ecg":1.04,"date":"2020-01-21T05:22:05.126Z"},]}]}

const result = Object.assign({}, source, {
  data: source.data[0].pasien_docs.map(Object.values)
})

console.log(result)

2 Comments

I am curious to learn the diffrence between your solution src.data[0].pasien_docs.map(Object.values) and source.data.map(v => v.pasien_docs.map(Object.values))[0], pls point out my mistake where I am going wrong in my case w.r.t mutating?
You're welcome. Another hint for you to make your code immutable (and yet not identical to mine ;), you may do Object.assign({}, source, {..
1

let obj = {
  status: true,
  message: "",
  data: [
    {
      pasien_docs: [
        {
          ecg: null,
          date: "2020-01-21T05:22:01.901Z",
        },
        {
          ecg: 1.03,
          date: "2020-01-21T05:22:02.979Z",
        },
        {
          ecg: 1.04,
          date: "2020-01-21T05:22:04.053Z",
        },
        {
          ecg: 1.04,
          date: "2020-01-21T05:22:05.126Z",
        },
      ],
    },
  ],
};

var finalobj = JSON.parse(JSON.stringify(obj));
var innerobj = obj.data;
var intermd = innerobj.map((data) => {
  return data.pasien_docs;
});
finalarray = intermd[0].map((val) => {
  return [val.ecg, val.date];
});
console.log(obj);
finalobj.data[0].pasien_docs=finalarray;
console.log(finalobj);

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.