0

I have an array of objects

{
    "tags": [{
            "id": "1",
            "data": [{
                    "bs": 1617779042313,
                    "bstp": 1617779099999,
                    "maxA": 1617779050311,
                    "maxV": 10,
                    "minA": 1617779050310,
                    "minV": 10,
                    "q": 3
                },
                {
                    "bs": 1617779100000,
                    "bstp": 1617779519999,
                    "maxA": 1617779100236,
                    "maxV": 10,
                    "minA": 1617779100231,
                    "minV": 10,
                    "q": 2
                }
            ]
        },
        {
            "id": "2",
            "data": [{
                    "bs": 1617779042313,
                    "bstp": 1617779099999,
                    "maxA": 1617779050311,
                    "maxV": 10,
                    "minA": 1617779050310,
                    "minV": 10,
                    "q": 3
                },
                {
                    "bs": 1617779100000,
                    "bstp": 1617779519999,
                    "maxA": 1617779100236,
                    "maxV": 10,
                    "minA": 1617779100231,
                    "minV": 10,
                    "q": 2
                }
            ]
        }

    ]
}

Output

{
    "tags": [{
            "id": "1",
            "data": [
                [1617779050311, 10, 3],
                [1617779100236, 10, 2]
            ]
        },
        {
            "id": "2",
            "data": [
                [1617779050311, 10, 3],
                [1617779100236, 10, 2]
            ]
        }

    ]
}

I want to format this array of objects into array of arrays

for the "data" key i need to get the value of specific keys and insert in array

specific keys - [MaxA, maxV, q]

How can I do it? Any small help appreciated..:)

efewfewfewfewfewfwefwefewfewfwefewfkfopkr3po2i50493543iropjgoprevm

3
  • What was the problem with your attempt? Are the specific keys dynamically determined, or are they fixed? Commented Apr 15, 2021 at 8:00
  • 1
    Welcome to Stack Overflow! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Apr 15, 2021 at 8:02
  • You need Array#map and Object.keys. Google their docs, attempt to solve this yourself and put it in the question. Commented Apr 15, 2021 at 8:04

1 Answer 1

1

You can use map. See the code snippet below:

const data = [
  {
    bs: 1617779042313,
    bstp: 1617779099999,
    maxA: 1617779050311,
    maxV: 10,
    minA: 1617779050310,
    minV: 10,
    q: 3,
  },
  {
    bs: 1617779100000,
    bstp: 1617779519999,
    maxA: 1617779100236,
    maxV: 10,
    minA: 1617779100231,
    minV: 10,
    q: 2,
  },
];

const keys = ["maxA", "maxV", "q"];

console.log(data.map((d) => keys.map((k) => d[k])));

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

2 Comments

Hi Som, I want to edit output from input provided in question, any small help appreciated.. :)
@MitulPanchal It would better if you ask a separate question as you've completed changed the input and output, you can share a link to that question I would be happy to help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.