HarryKant
HarryKant

Reputation: 55

How can I prevent object to get converted to string while adding both together?

I am trying to write a logic to modify my array which contains a list of objects. ex:

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

So that I will get the final result like below:

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

I want the ITEM name to be displayed before the object as given above. My logic is like below:

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

When I console it I receive the following :

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

Please tell me how can I stop the object from getting converted to a string.

Upvotes: 0

Views: 587

Answers (3)

James Coyle
James Coyle

Reputation: 10418

Loop over the array and add each to an object:

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)

Upvotes: 1

kockburn
kockburn

Reputation: 17626

There seems to be a misunderstanding regarding what JSON is (Javascript Object Notation). It's simply the format of the data that is being sent.

What you're working with is simply an array / object.

You should never modify a stringified object to add data. Work with the objects themselves and then stringify it.

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

Non-reduce version:

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

Upvotes: 1

Ajay Gaur
Ajay Gaur

Reputation: 5280

you can use reduce for the conversion

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

Upvotes: 1

Related Questions