我有一个对象,我想从给定的对象中分离出对象

时间:2018-05-04 12:53:29

标签: javascript

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",
}

我有一个Object,我想分开对象。所有具有最后一个数字0的键值具有自己的json,对于1它具有自己的json。

我想要这个结果

[{
  "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",
}]

4 个答案:

答案 0 :(得分:1)

试试这个

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);

更新:

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);

答案 1 :(得分:1)

嘿,如果您想尝试一下,我有一个可行的解决方案:

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);

答案 2 :(得分: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",
}

您希望根据键上的最后一个字符分隔属性。

因此,您创建一个包含结果的数组,然后使用Object.keys

循环输入键

对于每个键,分隔最后一个字符,因为这将是您的结果索引。还要检查结果数组是否包含该索引处的对象,否则创建一个空对象。

然后你有这个:

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];
})

此解决方案适用于任何输入对象,因为键/属性的名称末尾有索引。

答案 3 :(得分:0)

您可以使用reduce函数,如下所示:

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;
}, []);