如何在将两个对象加在一起的同时防止对象转换为字符串?

时间:2019-03-06 13:44:53

标签: javascript string object

我正在尝试编写逻辑来修改包含列表的数组  对象。例如:

    [
        {
            "S.N.": "1",
            "ITEM": "CIGARETTES",
            " QUANTITY SOLD ": " 3,603,221 ",
            "UNIT OF MEASURE": "CARTONS"
        },
        {
            "S.N.": "2",
            "ITEM": "LIQUOR",
            " QUANTITY SOLD ": " 5,680,586 ",
            "UNIT OF MEASURE": "BOTTLES"
        },
        {
            "S.N.": "3",
            "ITEM": "BEER",
            " QUANTITY SOLD ": " 7,581,446 ",
            "UNIT OF MEASURE": "CANS"
        }]

这样我将得到如下最终结果:

    "CIGARETTES": {
            "S.N.": "1",
            "ITEM": "CIGARETTES",
            "QUANTITY": " 3,603,221 ",
            "UNIT": "CARTONS"
        },
        "LIQUOR": {
            "S.N.": "2",
            "ITEM": "LIQUOR",
            "QUANTITY": " 5,680,586 ",
            "UNIT": "BOTTLES"
        },
        "BEER": {
            "S.N.": "3",
            "ITEM": "BEER",
            "QUANTITY": "7,581,446 ",
            "UNIT": "CANS"
        }

我希望将ITEM名称显示在上述对象之前。 我的逻辑如下:

    var callMe = function(){
    var newJson = json.map(function(item){
        var newItem = JSON.stringify(JSON.parse(item));
        newItem = item.ITEM + ':' + {item};
        return console.log(newItem)
    })
}

当我进行控制台操作时,会收到以下消息:

 CIGARETTES:[object Object]
 LIQUOR:[object Object]
 BEER:[object Object]
 GOLD:[object Object]
 WATCHES:[object Object]

请告诉我如何阻止对象转换为字符串。

3 个答案:

答案 0 :(得分:1)

您可以使用reduce进行转化

const   x = [
        {
            "S.N.": "1",
            "ITEM": "CIGARETTES",
            " QUANTITY SOLD ": " 3,603,221 ",
            "UNIT OF MEASURE": "CARTONS"
        },
        {
            "S.N.": "2",
            "ITEM": "LIQUOR",
            " QUANTITY SOLD ": " 5,680,586 ",
            "UNIT OF MEASURE": "BOTTLES"
        },
        {
            "S.N.": "3",
            "ITEM": "BEER",
            " QUANTITY SOLD ": " 7,581,446 ",
    }];
    
    const y = x.reduce(function(acc, item){
	acc[item.ITEM]= item;
	return acc;
}, {})

console.log(y);

答案 1 :(得分:1)

关于什么是JSON(Javascript对象表示法)似乎存在误解。它只是正在发送的数据的格式。

您正在使用的只是一个数组/对象。

绝对不要修改字符串对象以添加数据。使用对象本身,然后对其进行字符串化。

const data=[{"S.N.":"1","ITEM":"CIGARETTES"," QUANTITY SOLD ":" 3,603,221 ","UNIT OF MEASURE":"CARTONS"},{"S.N.":"2","ITEM":"LIQUOR"," QUANTITY SOLD ":" 5,680,586 ","UNIT OF MEASURE":"BOTTLES"},{"S.N.":"3","ITEM":"BEER"," QUANTITY SOLD ":" 7,581,446 ","UNIT OF MEASURE":"CANS"}]

const res = data.reduce((a,c)=>{
  a[c.ITEM] = c;
  return a;
}, {});

console.log(res);

//then JSON.stringify res

非简化版本:

const data=[{"S.N.":"1","ITEM":"CIGARETTES"," QUANTITY SOLD ":" 3,603,221 ","UNIT OF MEASURE":"CARTONS"},{"S.N.":"2","ITEM":"LIQUOR"," QUANTITY SOLD ":" 5,680,586 ","UNIT OF MEASURE":"BOTTLES"},{"S.N.":"3","ITEM":"BEER"," QUANTITY SOLD ":" 7,581,446 ","UNIT OF MEASURE":"CANS"}]

const res = {};
for(let i = 0; i < data.length; i++){
  res[data[i].ITEM] = data[i];
}

console.log(res);

//then JSON.stringify res

答案 2 :(得分:1)

遍历数组并将每个数组添加到对象:

const items = [{
    "S.N.": "1",
    "ITEM": "CIGARETTES",
    " QUANTITY SOLD ": " 3,603,221 ",
    "UNIT OF MEASURE": "CARTONS"
  },
  {
    "S.N.": "2",
    "ITEM": "LIQUOR",
    " QUANTITY SOLD ": " 5,680,586 ",
    "UNIT OF MEASURE": "BOTTLES"
  },
  {
    "S.N.": "3",
    "ITEM": "BEER",
    " QUANTITY SOLD ": " 7,581,446 ",
    "UNIT OF MEASURE": "CANS"
  }
]

const output = {}

items.forEach(i => {
  output[i.ITEM] = i
})

console.log(output)