How Split and Find Text Value in Array - javascript

How can I spilt below Array and print verificationCode= "value" in consle.log
var backup = [{kind=admin#directory#verificationCode, etag="HIWxtAjmBmqPQjjDV2Duo181uSc/ojhx3q8-0EqFY6M1x0NOJ85j1xg", userId=116800256069112846055, verificationCode=37140335}]

You need to correct your JS object to be in this format here.
var backup = [
{kind:"admin#directory#verificationCode",
etag:"HIWxtAjmBmqPQjjDV2Duo181uSc/ojhx3q8-0EqFY6M1x0NOJ85j1xg",
userId:"116800256069112846055",
verificationCode:"37140335"}
]
and then you can use the following to access your values
backup[0].verificationCode;

Related

JSON to JS array

JSON:
[{"CDATE":"0000-00-00","DISTANCE":"0"},
{"CDATE":"0000-00-00","DISTANCE":"1"},
{"CDATE":"0000-00-00","DISTANCE":"2"},
{"CDATE":"0000-00-00","DISTANCE":"3"}]
Is it possible to put all the distance value in their own array in JS? The formatting will be consistent as it comes from an API and is always formatted correctly.
What I've tried:
var arry = [ /* JSON noted above */ ];
alert(arry[1])
and
var arry = JSON.parse([ /* JSON noted above */ ]);
alert(arry[1])
Expected:
1
Actual:
{"CDATE":"0000-00-00","DISTANCE":"1"}
and the other gives an error.
I would like to extract just the DISTANCE value as an array of DISTANCE
I've tried JSON.parse() but I don't think this is what I am after as it hasn't worked for me.
Use .map
var data = [{"CDATE":"0000-00-00","DISTANCE":"0"},
{"CDATE":"0000-00-00","DISTANCE":"1"},
{"CDATE":"0000-00-00","DISTANCE":"2"},
{"CDATE":"0000-00-00","DISTANCE":"3"}];
var distance = data.map(el => el.DISTANCE);
console.log(distance[2]);
console.log(distance);
It is already a valid json so you do not need to use JSON.parse()

JavaScript return object property value

I have this object:
{"Header":["Date","Test1","Test2","Test3","N/A","Test4","Test5"],
"Values":[["Total Unique","79 280","1 598","5 972","20","2 633","9 696"],
["2017-06-19","28 026","1 036","3 667","20","1 097","4 672"]]}
My desired result is this:
Date
2017-06-19
What I was able to achieve:
Date ["2017-06-19","28 026","1 036","3 667","20","1 097","4 672"]
Using this code:
vm.header = data.Header[0];
vm.data1 = data.Values[1];
Because data.Values is a two-dimensional array you can get the desired result by changing the code to:
vm.header = data.Header[0];
vm.data1 = data.Values[1][0];
Header[0] = 'Date';
Header[1]= 'Test1';
Header[2]= 'Test2';
Header[3]= 'Test3';
Header[4]= 'N/A';
Header[5]= 'Test4';
Header[6]= 'Test5';
Values is 2D array
Values[0] = ["Total Unique","79 280","1 598","5 972","20","2 633","9 696"]
Values[1]=["2017-06-19","28 026","1 036","3 667","20","1 097","4 672"]
What you have tried so far is data.Header[0] would give you 'Date'. data.Values[1] would give you whole array. So you need to get "2017-06-19" you have to get first element of that array ie data.Values[1][0]

set array as value in json format in Javascript

I want to add array as json value.
Json format is as follows.
json_data = [
'name':'Testing'
'email':'TestEmail'
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com']
]
How can I set value of 'links' in javascript like that?
I did as follows.
links_array = [];
links_array =['testing','test2'];
json_data.links = links_array;
I wanted to append these two string but couldn't.
Any help would be appreciate.
Assuming that the syntax of your example is correct, you can use the "push" method for arrays.
json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[]
};
json_data.links.push("test1#test.com");
json_data.links.push("test2#test.com");
json_data.links.push("test3#test.com");
You have to make little changes to make it work.
First thing, You have to replace initial square brackets with curly one. By doing this your object will become JSON Literal - a key value pair.
Second thing, You have missed commas after 'name':'Testing' and 'email':'TestEmail'
Below will work perfectly:
var json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com']
}
In addition to push as mentioned by #giovannilobitos you can use concat and do it all in one go.
var json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com'
]
};
var links_array = ['testing','test2'];
json_data.links = json_data.links.concat(links_array);
console.log(json_data.links);
On MDN's array reference you can find a more complete list of how to modify arrays in JavaScript.

Get access to array inside object in javascript

I am new to angularjs so I dont know how to push any value to array at particular position. For example :
$scope.atparticular.push =
[
{questionText: 'queston1',questionId: '1',answerlst:[{answerText:'answer1',answerId: 'a1'},{answerText: 'answer2',answerId: 'a2'},{answerText: 'answer3',answerId: 'a3'}]},
{questionText: 'row2queston1',questionId: 'rq1',answerlst:[{answerText:'row2answer1',answerId: 'row2a1'},{answerText: 'row2answer2',answerId: 'row2a2'},{answerText: 'row2answer3',answerId: 'row2a3'}]}
];
If this is my object containing list and if I want to append answerlst with one more position containing empty values such as :
$scope.atparticular.push =
[
{questionText: 'queston1',questionId: '1',answerlst:[{answerText:'answer1',answerId: 'a1'},{answerText: 'answer2',answerId: 'a2'},{answerText: 'answer3',answerId: 'a3'},{answerText: '',answerId: ''}]},
{questionText: 'row2queston1',questionId: 'rq1',answerlst:[{answerText:'row2answer1',answerId: 'row2a1'},{answerText: 'row2answer2',answerId: 'row2a2'},{answerText: 'row2answer3',answerId: 'row2a3'},{answerText: '',answerId: ''}]}
];
Please give some suggestion. Thanks in advance.
If I understand correctly, you want to add an object at the end of the array answerlst of the first element of the array $scope.atparticular.push.
So you want:
$scope.atparticular.push[0].answerlst.push({answerText: '',answerId: ''});
Note that this has nothing to do with angularJS per se. It's just plain JavaScript.

Get value from json string using fields

I have a json string. I want to get the values from that string using the field name. Please help me to get this. This is my json string format.
[
{
"FLD_ID": 1,
"FLD_DATE": "17-02-2014 04:57:19 PM"
"FLD_USER_NAME": "DAFEDA",
"FLD_USER_EMAIL": "test#gmail.com",
"FLD_USER_PASS": "test"
}
]
I'm not really sure what the question is but how about
// assuming str is your JSON string
var obj = JSON.parse(str); // parse the string into an object
var firstObj = obj[0]; // get the first (and only) object out of the array
var fld_id = firstObj.FLD_ID; // you can access properties by name like this
var fld_date = firstObj['FLD_DATE']; // or like this
your JSON was invalid. I fixed it for you.
[{"FLD_ID":1,"FLD_DATE":"17-02-2014 04:57:19 PM", "FLD_USER_NAME":"DAFEDA","FLD_USER_EMAIL":"test#gmail.com","FLD_USER_PASS":"test"}]
here's a working example of how to alert the FLD_ID
<script>
var json = [{"FLD_ID":1,"FLD_DATE":"17-02-2014 04:57:19 PM", "FLD_USER_NAME":"DAFEDA","FLD_USER_EMAIL":"test#gmail.com","FLD_USER_PASS":"test"}];
alert(json[0].FLD_ID);
</script>
By the way, this is an array with 1 JSON object, which is why you must reference the index, 0 in this case.

Categories

Resources