How to remove an object from JSON response object [duplicate] - javascript

This question already has answers here:
Find and remove objects in an array based on a key value in JavaScript
(14 answers)
Closed 8 years ago.
I have a JSON response that appears as shown below. What I am trying to accomplish remove an object from the vaiable with a specifc key value pair using javascript
JSON Response.
console.log(userJSON); //response is below.
[Object {startDate="1403496000", name="user10"},
Object {startDate="1401681600", name="user11"},
Object {startDate="1423544400", name="user12"},
Object {startDate="1370836800", name="user13"},
Object {startDate="1370836800", name="user14"},
Object {startDate="1370750400", name="user15"},
Object {startDate="1402286400", name="user16"},
Object {startDate="1404273600", name="user17"}]
I want to be able to remove user10's object completly with the given variable name
Logic
var removeUser = user15;
//Do logic.
updatedUserJSON =
[Object {startDate="1403496000", name="user10"},
Object {startDate="1401681600", name="user11"},
Object {startDate="1423544400", name="user12"},
Object {startDate="1370836800", name="user13"},
Object {startDate="1370836800", name="user14"},
Object {startDate="1402286400", name="user16"},
Object {startDate="1404273600", name="user17"}]
Can someone help me get started on how to accomplish this using javascript? Would very much appreciate the help. Thank you.

var nameToDelete = 'user15';
for(var index=0; index < userJSON.length; index++)
{
if (userJSON[index].name == nameToDelete)
{
userJSON.splice(index,1);
break;
}
}

Related

Array to json using javascript [duplicate]

This question already has answers here:
JavaScript associative array to JSON
(5 answers)
Closed 4 years ago.
I am trying to pass an array through jQuery's ajax. The problem is that when I try to pass the array created in JavaScript to JSON, it returns something empty. I even try console.log, but when I try to convert it to JSON there is nothing. Here is a representation of how I do it:
var data = [];
data['name'] = 'test';
data['mail'] = 'test';
data['pass'] = 'test';
console.log(JSON.stringify(data)); // result []
Every array is an object. You are assigning object properties with the data['name'] = 'test' syntax. Arrays are indexed with integers and they "must" be in sequence. try a[0] = 'foo'. or Array.push

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

Get JSON key name [duplicate]

This question already has answers here:
Javascript get Object property Name
(4 answers)
Closed 6 years ago.
I have the following JSON data: {"success":"You are welcome"} that I have named json in my JavaScript code.
When I want to alert You are welcome I do json.success. So now the problem I am facing is that, what about if I want to alert success. Is there any way to get it?
So now the problem I am facing is that, what about if I want to alert
success. Is there a need way to get it ?
If your object is
var obj = {"success":"You are welcome"};
You can get the array of keys as
var keys = Object.keys(obj);
and then print it as
console.log( keys[ 0 ] ); //or console.log( keys.join(",") )
var obj = {"success":"You are welcome"};
var keys = Object.keys(obj);
console.log(keys[0]);
You mean something like this?
keys = Object.keys(json_object)
key_to_use = keys[0];
Try this code
alert(Object.keys({"success":"You are welcome"})[0]);
Since you're able to do json.success, you don't have "JSON data", you have a Javascript Object. JSON, or JavaScript Object Notation, is no more than the serialization of a Javascript object.
As other answers have stated, you can use Object.keys() to list the fields of an object.
Object.keys() can be called on any JavaScript object to get back a list of keys.

How to get the value dynamically from JSON object [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 7 years ago.
I want to retrieve a JSON value dynamically from JSON object. Below is the code i am using to get the value from JSON object.
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
jsonSplit = jsonToFind.split(htmlSplit+".")[1].trim();
console.log(jsonObj+"."+jsonSplit);
But I am getting [object Object].ensighten_tag.
Here ensighten_tag is the dynamic key value.
Can anyone suggest me how to get the value dynamically ?
console.log(JsonObj[jsonSplit])

Updating a result in JSON with a dynamic variable name [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]

Categories