how to add repeating strings value in json array [duplicate] - javascript

This question already has answers here:
Adding a new array element to a JSON object
(6 answers)
Closed 7 years ago.
I have JSON as
var newJSON = [{"key":"India","value":"72"},{"key":"India","value":"27"},{"key":"Pakistan","value":"90"},{"key":"Zimbamwe","value":"88"},{"key":"India","value":"100"},{"key":"Pakistan","value":"172"}]
I want desired result as
[{"key":"India","value":"199"},{"key":"Pakistan","value":"262"},{"key":"Zimbamwe","value":"88"}]
Please help me with this

are you sure you want them as {"india","20"}??
or is it {"india":"20"} //this is recommended
then you can do
json["india"]
or json[0]
Update:
var yourObj={key:'india', value:72};
var newObj = {};
newObj[yourObj.key] = yourObj.value;
newObj["Austria"]=100; // adds as a new list object

You could use
var jsondata =[];
var firstvalue = {country:"India",number:"200"};
jsondata.push(firstvalue);
var secondvalue = {country:"Australia",number: "210"};
jsondata.push(secondvalue);
for(var i =0; i< jsondata.length;i++){
console.log(jsondata[i].country);
}

Related

Is there a way to convert string to json object in javascript [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 4 years ago.
I am trying to convert list of string into json object.
My string is : personName + rank.
This is string is arranged as per the person's rank.
PS: the backend code is in solidity
My code so far is as follows:
var ranks = new Array(5);
PersonNames = Object.keys(names);
ranks = contractInstance.dashboard.call(PersonNames);// function in solidity
var finalList = [];
for (var j = 0; j < ranks.length; j++){
var winList = web3.toAscii(ranks[j]);
var winCount = contractInstance.RanksFor.call(ranks[j]).toString();
var person = {};
person["name"] = winList[j];
console.log(winList[j]);
person["ranks"] = winCount[j];
finalList.push(person);
}
var myJSON = JSON.stringify(finalList);
Please let me know if I am making use of the correct syntax.
I am learning how to implement javascript. Any help would be appreciated. Thank you!!
I think using the provided method is the best way...
Example 1:
var json = JSON.parse('{ "hello":"world" }');
Example 2:
var json = '{"hello":"world"}';
obj = JSON.parse(json);

How to remove duplicated objects from an Array in javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I am using above code and removing the items based on id
I am getting id is not defined while comparing i+1 item when i reaches to maximum array length.
Appreciate your help....
var arr = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":474.5,"top":16},{"id":"0-block-2","left":686.5,"top":0}];
Expecting outout = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":686.5,"top":0}]
Array.prototype.unique = function(){
var passNum = this.length;
var _self = this;
// to repeat loops n*n times
while(passNum>0){
for(var i = 0;i<this.length;i++) {
if(this[i].id==this[i+1].id){
var _indx = this.indexOf(this[i]);
this.splice(_indx,1);
}
}
passNum = passNum-1;
}
return this;
};
You can do this with ES2015 Sets.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
EDIT: If you can't use ES2015, there is a polyfill available.

how to create array object using integer variable javascript [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I need to fill an array object as like below using integer variable.
for(i=1; i<=2; i++)
arr[i].push({i:(100 * i)})
Expected result is:
arr = [{ 1:100,2:200},{1:100,2:200}]
Problem is, array created as like below
arr = [{i:100,i:200},{i:100,i:200}]
You need to push to arr instead of arr[i].
Also, you can't use a variable as a key in json directly.
var arr = [];
for(i=1; i<=2; i++)
{
var b = {};
b[i] = 100*i;
arr.push({[i]:(i*100)});
}
console.log(arr);

Dynamically create js Array filled with certain value [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 7 years ago.
I'm wondering if there is a way to create javascript/jquery array in one-liner to receive something like:
my_array = ['-', '-', ,'-' ,'-']
Idea is that array should be created with dynamic length and all values filled with given value.
Thanks in advance!
Try:
var total = 4;
var my_array = new Array(total + 1).join("-").split("");
document.write(JSON.stringify(my_array))
.fill Support The native function will be added in (ECMAScript 6), but for now is not available.
if(!Array.prototype.fill){
Array.prototype.fill = function(val){
for (var i = 0; i < this.length; i++){
this[i] = val
}
return this
}
}
var my_array = new Array(4).fill("-"); //returns ["-","-","-","-"]
Try to use:
Array.apply(null, new Array(63)).map(String.prototype.valueOf,"-")

javascript convert array to json object [duplicate]

This question already has answers here:
JavaScript: Converting Array to Object
(2 answers)
Closed 8 years ago.
I've got the following array:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
i wanto convert it to json like this:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
So than i can store everything in mongodb in a unique document.
Regards,
Just run a loop and equate...like...
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
It will do
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
Because your JSON is invalid.
Not sure what you are asking
From array or another variable to json string =>
var str = JSON.stringify(thing);
From a json string to variable
var thing = JSON.parse(str);

Categories

Resources