-4

I have a object which contains the following data:

   {cost: 2
    field1: 2
    field2: 2
    field3: 2
    field4: 2
    length: 2
    material: 5715382975332352
    name: "name"
    stock: 2
    thickness: 2
    type: "RECTANGULAR"}

Now I want to change the field1, field2, field3 and field4 into something like:

parameters: [2, 2, 2, 2] so remove the field and turn them into a array with only the values, so the updated object should look like:

   {cost: 2
    parameters: [2,2,2,2] //values field1, field2, field3, field4
    length: 2
    material: 5715382975332352
    name: "name"
    stock: 2
    thickness: 2
    type: "RECTANGULAR"}

Note: that I can also change the field1, field2 etc.. names if this would be easier for sorting the array.

I tried the following but with no luck: Convert object's properties and values to array of key value pairs

Jan
  • 653
  • 1
  • 7
  • 22

4 Answers4

1

Steps:

  1. Create an array into data object with property named parameters.

  2. Push every data.field* into the data.parameters array.

  3. Delete every data.field* afterwards.

Without Loop

let data = {cost: 2,
    field1: 2,
    field2: 2,
    field3: 2,
    field4: 2,
    length: 2,
    material: 5715382975332352,
    name: "name",
    stock: 2,
    thickness: 2,
    type: "RECTANGULAR"
}

data.parameters = [data.field1, data.field2, data.field3, data.field4];
delete data.field1;
delete data.field2;
delete data.field3;
delete data.field4;
console.log(data);

With Loop

let data = {cost: 2,
    field1: 2,
    field2: 2,
    field3: 2,
    field4: 2,
    length: 2,
    material: 5715382975332352,
    name: "name",
    stock: 2,
    thickness: 2,
    type: "RECTANGULAR"
}

data.parameters = [];
for (let i = 1; i <= 4; i++){
  data.parameters.push(data["field"+i]);
  delete data["field"+i];
}
console.log(data);
holydragon
  • 6,158
  • 6
  • 39
  • 62
  • Btw, is there a way to count fields in this object and add this count inside the loop? Because objects contains less fields ex. field1, field2 – Jan Jan 11 '19 at 09:20
  • 1
    @holydragon An **[alternate way](https://jsfiddle.net/RajeshDixit/hjd97y0n/3/)** as your loop approach is restricting only till 4 keys. This is also not full proof but more scalable. – Rajesh Jan 11 '19 at 09:24
0

Get the entries from your input object and then reduce them to a new object, checking the props with key field and grouping them into a new prop parameters which is an array:

const data = {
  cost: 1,
  field1: 2,
  field2: 3,
  field3: 4,
  field4: 5,
  length: 6,
  material: 5715382975332352,
  name: "name",
  stock: 7,
  thickness: 8,
  type: "RECTANGULAR"
};

console.log(Object.entries(data).reduce((acc, val, ind) => {
  if (val[0].includes('field')) {
    acc['parameters'] = acc['parameters'] || [];
    acc['parameters'].push(val[1]);
  }
  else {
    acc[val[0]] = val[1];
  }
  return acc;
}, {}));
quirimmo
  • 9,800
  • 3
  • 30
  • 45
0

This will do-

    var a={cost: 2,
        field1: 2,
        field2: 2,
        field3: 2,
        field4: 2,
        length: 2,
        material: 5715382975332352,
        name: "name",
        stock: 2,
        thickness: 2,
        type: "RECTANGULAR"};
       var arr=Object.keys(a);
       var values=[];
       arr.forEach((e)=>{
    if(e.includes('field'))
    {
        
    values.push(a[e]);
    delete a[e];
    }
       })
       a.parameters=values;
       console.log(a);
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

You can do like this..

<script type="text/javascript">

    var obj = {
        cost: 2,
    field1: 2,
    field2: 2,
    field3: 2,
    field4: 2,
    length: 2,
    material: 5715382975332352,
    name: "name",
    stock: 2,
    thickness: 2,
    type: "RECTANGULAR"};

    var obj2 = {};
    var arr = [];

    for (var k in obj) {
        if(k.includes("field")){ // if key contains field text
            arr.push(obj[k]);
        } else {
            obj2[k] = obj[k];   
        }
    }
    obj2['parameters'] = arr;

    // your object
    console.log(obj);
    // your desired object
    console.log(obj2);


</script>
Pratik Rathod
  • 43
  • 1
  • 1
  • 7