This is the data displaying in console.log.
{"data":
[
{
"CloserName":null,
"agent_id":"10807",
"AgentName":"TEST",
"SurveyDate":"02/02/2018 02:18:46 AM",
"SurveyName":"Ruth ",
"state":"West Bengal",
"phone":"9836969715",
"status":"Approved",
"verification_progress":"Pending",
"survey_id":"1",
"rejection_remarks":"aa",
"tl_remarks":"Pending"
}
],
"count":1
}
Can anyone help me display a single value (i.e survey_id)? I just want to fetch that survey_id
Here's an example:
var json = {
"data":[
{
"CloserName":null,
"agent_id":"10807",
"AgentName":"TEST",
"SurveyDate":"02/02/2018 02:18:46 AM",
"SurveyName":"Ruth ",
"state":"West Bengal",
"phone":"9836969715",
"status":"Approved",
"verification_progress":"Pending",
"survey_id":"1",
"rejection_remarks":"aa",
"tl_remarks":"Pending"
}
],
"count":1
}
// get first id
var id = json.data[0].survey_id
console.log(id)
// get all ids
var ids = json.data.map(x => x.survey_id)
console.log(ids)
If the JSON is stringified, call JSON.parse(jsonStr) first.
You have to parse the JSON-String into an object. After that you can access the data with default object-identifiers.
const object = JSON.parse('{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}');
console.log(object.data[0].survey_id)
If your JSON data has been stringified (your sample JSON is a valid JSON object, not a string) you would first need to parse it, and then get the IDs (assuming you will have more than one item inside the data array) and log them out. There's a few different ways of achieving this:
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
let parsed = JSON.parse(stringified);
parsed = parsed.data.map(item => item.survey_id);
console.log(parsed);
You can also just loop over the items in the array and log them one by one using a for loop:
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
let parsed = JSON.parse(stringified);
for (let i = 0; i < parsed.data.length; i++) {
console.log(parsed.data[i].survey_id);
}
Or using a for-of:
const stringified = '{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}';
let parsed = JSON.parse(stringified);
for (const item of parsed.data) {
console.log(item.survey_id);
}
According to your current object structure
Your object
var obj = {"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1};
Fetching survey_id
obj.data[0].survey_id
Related
I want to parse this JSON array and print the salary so this what I have tried so far
there is nothing logged in the Console
if (data.action == 'SendArray') {
let invites = data.invites
const obj = JSON.parse(invites)
const myJSON = JSON.stringify(obj);
console.log(myJSON.salary)
}
JSON:
{"factioname":"sp-force","inviter":"MohammedZr","salary":5000},
{"factioname":"air-force", "inviter":"Admin","salary":8000}
This const myJSON = JSON.stringify(obj) turns your object back into a string, which you don't want.
I've done some setup to get your data matching your code but the two things you should note are:
Iterating through the array of invites using for .. of (you could use forEach instead) and
Using deconstruction to pull out the salary
data = { action: 'SendArray'
, invites: '[{"factioname":"sp-force","inviter":"MohammedZr","salary":5000},{"factioname":"air-force", "inviter":"Admin","salary":8000}]'
}
if (data.action == 'SendArray') {
let invites = data.invites
const obj = JSON.parse(invites)
for ({salary} of JSON.parse(invites))
console.log(salary)}
myJSON is an array of objects. To log in console need to get each object. we can use forEach to get each object and then can console log the salary key.
let myJSON =[
{"factioname":"sp-force","inviter":"MohammedZr","salary":5000},
{"factioname":"air-force", "inviter":"Admin","salary":8000}];
myJSON.forEach(obj=> {
console.log(obj.salary);
});
I was wondering how i could merge these two objects retrieve the tag values and store them in an array. This data is also coming from a json response so incoming data should be pushed onto the end of the new array.
so it would look something like this
["2011 LDI", "2012 LDI"]
array with incoming data:
["2011 LDI", "2012 LDI","2013 LDI"]
Here is what I am getting back in my console.log:
[19-08-25 21:58:32:055 PDT] []
[19-08-25 21:58:32:056 PDT] []
Here are the two objects of arrays i am trying to merge:
{date_added=2019-08-26 04:19:00.112083, tag=LDI 2011}
{date_added=2019-08-26 04:19:00.112089, tag=LDI 2012}
and I want it to look like this
[LDI 2011, LDI 2012]
and how I am trying to do it.
var tagtest = [];
var tags = message.student_detail.student_tags,
i = 0,
len = tags.length;
for (i; i < len; i++) {
var obj = tags[i];
for (a in obj) {
}
Array.prototype.push(tags, tagtest);
Logger.log(tagtest)
}
Based on your desired output ([LDI 2011, LDI 2012]), You may want the only tag values from the array, If this is what you are looking for then .map() will help you
const array = [
{
date_added: '2019-08-26',
tag: 'LDI 2011'
},
{
date_added: '2019-08-26',
tag: 'LDI 2012'
}];
const tags = array.map((r) => {
const chunk = r.tag.split(' ');
return `${chunk[1]} ${chunk[0]}`;
} );
console.log(tags);
A for in loop is a great way to work with objects. I updated the code above so that it was actually an array of objects, not an error. See below :)
var data = [{date_added: "2019-08-26 04:19:00.112083", tag: "LDI 2011"},
{date_added: "2019-08-26 04:19:00.112089", tag: "LDI 2012"}];
var newArr = [];
for(var item in data) {
newArr.push(data[item].tag);
}
console.log(newArr);
I currently have an item in local storage which looks like this
"cars":[
{
"Id":7,
"Name":"Audi",
},
{
"Id":8,
"Name":"Ford",
}
I want to retrieve all of the Id's only and store them in a string.
At the minute I am pulling the data like this:
var cars = "";
cars= localStorage.getItem('cars');
var carArr= new Array();
carArr.push(cars);
How can I just obtain the Id's
If i understand your question correctly, you have to use Array.map to transform your array in combination with JSON.parse and JSON.stringify to read/write from the storage.
Here is an example using a "mocked" localStorage:
// use a mock storage because snippet doesn't allow localStorage usage.
var mockStorage = {};
// setup initial storage
try {
mockStorage.cars = JSON.stringify([
{
Id:7,
Name:"Audi",
},
{
Id:8,
Name:"Ford",
}
]);
} catch(e) {}
console.log('inital local storage:\n', mockStorage);
// example
var cars = [];
// parse JSON string value from storage to javascript object.
try {
cars = JSON.parse(mockStorage.cars)
} catch(e) {}
console.log('cars:\n', cars);
// transform array of cars to array of car ids
var ids = cars.map(car => car.Id)
console.log('car ids:\n', ids);
// transform array to JSON string
try {
mockStorage.cars = JSON.stringify(ids);
} catch(e) {}
console.log('local storage:\n', mockStorage);
localStorage only supports strings. So, you have to use JSON.parse to get the cars array from string and then use array#map to get all the ids.
var carsString = localStorage.getItem('cars');
var cars = JSON.parse(carsString);
var ids = cars.map( car => car.Id);
console.log(ids);
Use this,
//you get this from localStorage after you parse it
//JSON.parse(localStorage.cars);
var cars = [
{
"Id":7,
"Name":"Audi",
},
{
"Id":8,
"Name":"Ford",
}];
var res = [];
cars.forEach(function(val){
res.push(val.Id);
});
console.log(res);
var data = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';
Here I have posted my JSON data. I want to get all the defaultValue in JSON/Array format. My output should be like-
defaultValues:['Size30','Black','Low']
How to manage that in the foreach loop?
my code :
var otherSelectedOption;
angular.forEach(data, function(optionValue, optionKey) {
if (optionValue.defaultValue.text) {
otherSelectedOption = (optionValue.defaultValue.text);
}
selectedOption = {defaultValues: otherSelectedOption};
console.log(selectedOption);
});
Your JSON is not valid, since objects are not separated by comma ,
Suppose this is the JSON
var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';
try
var arr = JSON.parse(obj).map( function(item){
return item.defaultValue;
});
[
[
{"path":"path2","value":"kkk"},
{"path":"path0","value":"uuu"},
{"path":"path1","value":"ppp"}
]
]
I get above result from for my manipulation, But I need it as follows.
["path":"path2","value":"kkk"],
["path":"path0","value":"uuu"],
["path":"path1","value":"ppp"]
Here is my code:
$scope.sharePaths[d.id] = []
d.conf.paths = []
$scope.sharePaths[d.id][index] = []
commaPath = 'kkk,uuu,ppp'
a = commaPath.split(',')
for key of a
value = a[key]
$scope.sharePaths[d.id][index].push {'path':'path'+key, 'value':a[key]}
d.conf.paths.push {'path':'path'+key, 'value':a[key]}
Just use the first element of your array. The variable data is already formatted correctly. The new format you want is not valid JSON.
var data = [[{"path":"path2","value":"kkk"},{"path":"path0","value":"uuu"},{"path":"path1","value":"ppp"}]];
data = data[0];