0

I have an Object which is having some properties like this:

obj1={
    "id": 2,
    "description": "",
    "operationIds": [
        {
            "id": 1,
            "name": "Standard"
        }
    ],
    "ratingIds": [
        {
            "id": 1,
            "name": "name1",
            "description": "",
        },
        {
            "id": 4,
            "name": "name4",
            "description": "",
        },
        {
            "id": 8,
            "name": "name8",
            "description": "",
        },
    ],
}

I want covert the array of objects (operationIds and ratingIds) inside the object to array of properties, I'm receiving this object and I want to apply the change on it and supply another method so it should look like this:

obj1={
    "id": 2,
    "description": "",
    "operationIds": [
        1
    ],
    "ratingIds": [
        1,
        4,
        8
    ],
    "timestamp": "AAAAAAAGJ6c=",
    "estimatedUtilReconciliationApplies": true
}

I was able to do it but in a verry ugly way, is there a more simple and clear way to accomplish this ?

let x = {...obj} as any;
let ar1 = x.operationIds;
const arr1= ar1.map(function (obj) {
  return obj.id;
});

let ar2 = x.ratingIds;
const arr2= ar2.map(function (obj) {
  return obj.id;
});

x.operatingEnvironmentIds = arr1;
x.thrustRatingIds = arr2;
3
  • What makes your approach "ugly"? Commented Dec 7, 2022 at 16:57
  • Your solution is perfectly fine. Commented Dec 7, 2022 at 16:58
  • forget about naming I'm just testing, but its long and I'm wondering if there is an easy and short solution for this. Commented Dec 7, 2022 at 16:59

2 Answers 2

2

You can use spread operator and map

let obj1={
    "id": 2,
    "description": "",
    "operationIds": [
        {
            "id": 1,
            "name": "Standard"
        }
    ],
    "ratingIds": [
        {
            "id": 1,
            "name": "name1",
            "description": "",
        },
        {
            "id": 4,
            "name": "name4",
            "description": "",
        },
        {
            "id": 8,
            "name": "name8",
            "description": "",
        },
    ],
}

console.log({
    ...obj1,
    operationIds:obj1.operationIds.map(elem => elem.id),
    ratingIds:obj1.ratingIds.map(elem => elem.id),
})

And as a function

let obj1={
    "id": 2,
    "description": "",
    "operationIds": [
        {
            "id": 1,
            "name": "Standard"
        }
    ],
    "ratingIds": [
        {
            "id": 1,
            "name": "name1",
            "description": "",
        },
        {
            "id": 4,
            "name": "name4",
            "description": "",
        },
        {
            "id": 8,
            "name": "name8",
            "description": "",
        },
    ],
}
let transform = (obj) => {
    return({
        ...obj,
        operationIds:obj.operationIds.map(elem => elem.id),
        ratingIds:obj.ratingIds.map(elem => elem.id),
    })
}
let transformed = transform(obj1)
console.log(transformed)

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

Comments

0

We loop the array and use the Object.assign() method to convert an array of objects to a single object. This merges each object into a single resultant object.

The Object.assign() method also merges the properties of one or more objects into a single object.

1 Comment

Maybe you could make a concrete code example?

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.