converting a multidimentional JSON array to JS variables - javascript

imagine you have the following JSON:
{"zero":{"0":"foo","1":"foo2"},"one":"fooa","two":"foob"}
What is the most efficient way to convert it to :
var zero = ['foo','foo2'];
var one = 'fooa';
var two = 'foob';

JSON.parse:
var json = '{"zero":{"0":"foo","1":"foo2"},"one":"fooa","two":"foob"}';
var pJson = JSON.parse(json);
var zero = [ pJson.zero[0], pJson.zero[1] ];
var one = pJson.one;
var two = pJson.two;

Related

Combine array in JavaScript with underscore character

Var a = "837297,870895"
Var b = "37297,36664"
OutPut = "837297_37297,870895_36664
I tried multiple script but does not working
You can split and map the splitted array as such:
var a = "837297,870895"
var b = "37297,36664"
var output = a.split(',').map((e,i)=> e+"_"+b.split(',')[i])
console.log(output.toString())
For ES2015 or lower
var a = "837297,870895"
var b = "37297,36664"
var output = a.split(',').map(function(e,i){return e+"_"+b.split(',')[i]})
console.log(output.toString())
You can start by converting the two strings (lists) into arrays using .map() and .split() methods.
Then use .map() and .join() methods to produce the desired string (list):
const a = "837297,870895";
const b = "37297,36664";
const [c,d] = [a,b].map(e => e.split(','));
const output = c.map((e,i) => `${e}_${d[i]}`).join();
console.log( output );

Create JSON object out of values

I have these values:
var firstHeader = $('#header0').text(); // 6KP
var secondHeader = $('#header1').text(); // 7KP
var thirdHeader = $('#header2').text(); // 8KP
var first = $('#input0').val(); // 77
var second = $('#input1').val(); //88
var third = $('#input2').val(); // 99
And I need to create a JSON object to have this:
{6KP:77, 7KP:88, 8KP:99}
I tried to do this but without success:
var data = { firstHeader: first, secondHeader: second, thirdHeader: third };
data = JSON.stringify(data, null, ' ');
And my response is this:
{firstHeader: 77, secondHeader:88, thirdHeader:99}
How can I put the values instead of these var names?
You use array notation, e.g.,
var data = {};
data[firstHeader] = first;
If you're using ES6 you can use the shorthand:
var data = {
[firstHeader]: first,
// Etc.
};
You need to use square brackets to evaluate the variable value as a key name:
var data = {};
data[firstHeader] = first;
data[secondHeader] = second;
data[thirdHeader] = third;
WORKING EXAMPLE
var firstHeader = '6KP';
var secondHeader = '7KP';
var thirdHeader = '8KP';
var first = '77';
var second = '88';
var third = '99';
var data = {};
data[firstHeader] = first;
data[secondHeader] = second;
data[thirdHeader] = third;
console.log(JSON.stringify(data));
Replace below line
var data = { firstHeader: first, secondHeader: second, thirdHeader: third };
with
var data ={};
data[firstHeader]= first;
data[secondHeader]= second;
data[thirdHeader]: third ;

How to get multiple matched values from the array object using javascript?

How to get multiple matched values from the array object?
my scenario is:
var studentData = [
{name:"Ram",htno:1245},
{name:"mohan",htno:1246},
{name:"madhu",htno:1247},
{name:"ranga",htno:1248}
];
var studentsToCompare = ["mohan","ranga"];
Expected output:
var matchedStudentsdata = [
{name:"mohan",htno:1246},
{name:"ranga",htno:1248}
];
Thanks
Rambabu Bommisetti
Use filter on student array:
var student = [{name:"Ram",htno:1245},{name:"mohan",htno:1246},{name:"madhu",htno:1247},{name:"ranga",htno:1248}]
var matchedvalues = ["mohan","ranga"];
var matchedStudentsdata = student.filter(function(stu) {
return matchedvalues.indexOf(stu.name) > -1;
})
console.log(matchedStudentsdata)
Try with filter() like the following:
var student = [{name:"Ram",htno:1245},{name:"mohan",htno:1246},{name:"madhu",htno:1247},{name:"ranga",htno:1248}]
var matchedvalues = ["mohan","ranga"];
var matchedStudentsdata = student.filter(stu => matchedvalues.includes(stu.name));
console.log(matchedStudentsdata);

Why does it say input data array is not in correct format fro jqchart?

My data is in this format as belwo;
i.e mymy= UnitedStates,65|UnitedKingdom,58|Germany,30|India,60|Russia,65|China,75
Code :
var myarr = mymy.split("|");
var i=0;
var myarr2 = [];
for (i=0;i<myarr.length;i++)
{
var mytemp=myarr[i].split(",");
myarr2[i]=mytemp[0]+','+mytemp[1];
};
var myChart = new JSChart('chartcontainer','bar');
var myData2 = new Array(myarr2);
/*
where mow
myarr2[0]={UnitedStates,65}
myarr2[1]={UnitedKingdom,58}
and so on
*/
myChart.setDataArray(myData2);
myChart.draw();
It looks like dataArray has to be an array of data points, each being an array having label and value as members. So I guess it'd be:
var mymy = 'UnitedStates,65|UnitedKingdom,58|Germany,30|India,60|Russia,65|China,75'
var myarr = mymy.split("|");
var myarr2 = [];
for(i=0;i<myarr.length;i++){
var mytemp=myarr[i].split(",");
myarr2[i] = [mytemp[0],mytemp[1]];
};
var myChart = new JSChart('chartcontainer','bar');
myChart.setDataArray(myarr2);
myChart.draw();

Handling of array; returned by methods in iOS UI Automation

I have been able to use all the methods for automating iphone app test except with ones which returns array... e.g elements()
I have tried to do it using declaration of array as
var arr = [];
var arr = UIATarget.localTarget().frontMostApp().mainWindow().tabBar().elements();
UIALogger.logPass("result"+ arr[0]) // just to get first element
But it is not working
Can someone ans how to handle array. What is the correcting required?
What exactly do you want from such array?
Here is an example how to handle array of elements:
function getAllNamesInList (list, index){
var elem_list = list[index].elements();
var elem_count = elem_list .length;
var names = [];
var elem_name;
for (var elem_ind = 0; elem_ind < elem_count ; elem_ind++){
elem_name= elem_list [cell_ind].name();
if (!elem_name){fail ("TEST_INFO: Empty Element name!!!");}
names.push(elem_name);
}
return names;
};
Here is usage example of this function():
Your case:
var app = UIATarget.localTarget().frontMostApp();
var window = app.mainWindow();
var arr = window.tabBar()
var current_names = [];
current_names = getAllNamesInList (arr , 0);
UIALogger.logMessage ("Here are ALL names from array " + current_names );
Other possible lists which can be transferred and used within this function():
var table_views = window.tableViews();
var tab_bar = app.tabBar();
var nav_bar = app.navigationBar();

Categories