0

I use postman call my api and here is the response.

I want to modify this object key into "id" and value is auto increment(1,2,3....), the value of original key( "amount 100~1000": 2) as "count" like expect to get down below.

data = {
        "amount 100~1000": 2,
        "amount 1001~2000": 4,
        "amount 2001~3000": 1,
        "amount 3001~4000": 2,
}

here is what i tried, i really don't know how to do this using for loop (or maybe forEach is better way?)

const x = Object.values(data[0])

let id = 1;
let count;
let arr = [];
for (let index = 0; index < x.length; index++) {

    id++

    const obj = { id: index }
    arr.push(obj)
}
console.log(arr);// [ { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 } ]

expect to get:

{
    "id":1, // amount 100~1000
    "count": 2,
},
{
    "id":2, // amount 1001~2000
    "count": 4,
},
{
    "id":3, // amount 2001~3000
    "count": 1,
},
.
.
.

3 Answers3

1

Re-edit: After the comments and follow-up questions I am adding a solution that will keep the values in the array sorted by the input data's keys. I am not sure this is what you intend but just for the sake of completeness.

const input = data[0];
const keys = Object.keys(input);
const output = [];
keys.sort((a, b) => a.localeCompare(b, { numeric: true })).forEach((key, idx) => {
    output.push({id: idx+1, count: input[key]})
})
console.log(output);
Nir Levy
  • 4,613
  • 2
  • 34
  • 47
  • 1
    To add to this, while it may work in the example [the order of properties in JS is not guaranteed](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order), so you may end up with "amount 1001-2000" with an ID other than 2. – kil Nov 07 '22 at 08:50
  • @kil, as of ES2020 it should: https://stackoverflow.com/a/30919039/130304 – Nir Levy Nov 07 '22 at 09:04
  • 1
    @NirLevy All that guarantees is that certain rules maintain order, but depending on how the object is created the keys could still be in an arbitrary order. Better to explicitly sort. – pilchard Nov 07 '22 at 09:05
  • @pilchard I agree, yes. but the properties in the OP's question are not really sortable without signification manipulation which I am not sure I have the right tool to do without understanding the context. – Nir Levy Nov 07 '22 at 09:07
  • 1
    `sort((a, b) => a.localeCompare(b, { numeric: true }))` – pilchard Nov 07 '22 at 09:22
  • 1
    @NirLevy you learn something new every day, good to know! – kil Nov 07 '22 at 09:27
  • thanks guys, so after modified the data i still need use sort((a, b) => a.localeCompare(b, { numeric: true })) this to make sure the order is right? – user19304185 Nov 07 '22 at 10:39
  • user19304185 I've edited the answer to show you how to sort the data based on @pilchard's suggestion. I know you already accepted an answer but the accepted answer will not sort the data. This might be fine in your case, but just wanted to be clear. – Nir Levy Nov 07 '22 at 11:18
0

Here's my solution to your question:

let data = {
        "amount 100~1000": 2,
        "amount 1001~2000": 4,
        "amount 2001~3000": 1,
        "amount 3001~4000": 2,
}

let list = []

let dataValuesList = Object.values(data)

for(let i = 1; i <= dataValuesList.length;i++){
    list.push({
        id: i,
        count: dataValuesList[i-1]
    })
}

console.table(list)
0

You can implement this with Javascript Object built-in function Object.keys() or Object.values by simply looping them together:

const data = {
        "amount 100~1000": 2,
        "amount 1001~2000": 4,
        "amount 2001~3000": 1,
        "amount 3001~4000": 2,
}

const modifiedData = []

for (let i = 0; i < Object.keys(data).length; i++) {
    modifiedData.push({id: i + 1, count: Object.values(data)[i] })
}

console.log(modifiedData)

selim
  • 201
  • 2
  • 8