0
const myjson = {
  "productname0": "Test1",
  "price0": "10",
  "categories0": "61",
  "type0": "Physical",
  "availability0": "available",
  "weight0": "0.5",
  "productname1": "Test2",
  "price1": "20",
  "categories1": "61",
  "type1": "Physical",
  "availability1": "available",
  "weight1": "0.7",
}

I have an Object and I want to separate object. All the key value which has last number 0 it has own json, for 1 it has own json.

I want this result

[{
  "productname0": "Test1",
  "price0": "10",
  "categories0": "61",
  "type0": "Physical",
  "availability0": "available",
  "weight0": "0.5",
},
{
  "productname1": "Test2",
  "price1": "20",
  "categories1": "61",
  "type1": "Physical",
  "availability1": "available",
  "weight1": "0.7",
}]
Ankit Parmar
  • 670
  • 10
  • 24

4 Answers4

1

Try like this

const myjson = {
  "productname0": "Test1",
  "price0": "10",
  "categories0": "61",
  "type0": "Physical",
  "availability0": "available",
  "weight0": "0.5",
  "productname1": "Test2",
  "price1": "20",
  "categories1": "61",
  "type1": "Physical",
  "availability1": "available",
  "weight1": "0.7",
}

var finalArray=[];
Object.keys(myjson).forEach(function(value){
if(value.endsWith("0")){
    if(!finalArray[0]){
        finalArray[0]={};
    }
    finalArray[0][value]=myjson[value];
} else if(value.endsWith("1")) {
    if(!finalArray[1]){
        finalArray[1]={};
    }
    finalArray[1][value]=myjson[value];
}

});
console.log(finalArray);

Update :

const myjson = {
  "productname0": "Test1",
  "price0": "10",
  "categories0": "61",
  "type0": "Physical",
  "availability0": "available",
  "weight0": "0.5",
  "productname1": "Test2",
  "price1": "20",
  "categories1": "61",
  "type1": "Physical",
  "availability1": "available",
  "weight1": "0.7",
}

var finalArray=[];
Object.keys(myjson).forEach(function(value){

var lastChar = value[value.length -1];

    if(!finalArray[lastChar]){
        finalArray[lastChar]={};
    }
    finalArray[lastChar][value]=myjson[value];

});
console.log(finalArray);
thaveethu gce
  • 585
  • 1
  • 9
  • 20
1

Hey I have a working solution for you if you want to try it :

var Data = [];
for (var i = 0; i < (Object.keys(myjson).length) / 6; i++) 
{ 

Data.push({ 
"productname": myjson["productname" + i], 
"price": myjson["price" + i], 
"categories": myjson["categories" + i],
 "type": myjson["type" + i], 
"availability": myjson["availability" + i], 
"weight": myjson["weight" + i] 
}); 

}
console.log(Data);
TBenchikhi
  • 11
  • 2
0

Let's say you have this input :

const myJSON = {
  "productname0": "Test1",
  "price0": "10",
  "categories0": "61",
  "type0": "Physical",
  "availability0": "available",
  "weight0": "0.5",
  "productname1": "Test2",
  "price1": "20",
  "categories1": "61",
  "type1": "Physical",
  "availability1": "available",
  "weight1": "0.7",
}

You want to separate the properties according to the last character on the keys.

So you create an array containing your result, and them loop over the input's keys using Object.keys

For each key, separate the last character, because this will be your result index. Also check if the result array contains an object at that index already, or create an empty object if not.

Then you have this :

const result = [];

Object.keys(myJSON).forEach(key => {
    if (result[key[key.length-1]] === undefined) {
        result[key[key.length-1]] = {};
    }
    result[key[key.length-1]][key] = myJSON[key];
})

This solution will work for any input object, given that the keys / properties have the index at the end of their name.

Seblor
  • 6,947
  • 1
  • 25
  • 46
0

You could make use of the reduce function, something like this:

const parameterNames = ['productname', 'price', 'categories', 'type', 'availability', 'weight'];
const data = Object.keys(myjson).reduce((acc, key) => {
  let parameterName = null;
  for (let i = 0; i < parameterNames.length; i++) {
      if (key.indexOf(parameterNames[i]) > -1) {
         parameterName = parameterNames[i];
         break;
      }
  }

  if (parameterName) {
    let index = key.split(parameterName)[1];

    if (acc[index] === undefined) {
      acc[index] = {};
    }

    acc[index][parameterName] = myjson[key];
  }    

  return acc;
}, []);
magritte
  • 7,396
  • 10
  • 59
  • 79