So this is a little trippy, but here goes.
Let's say I have two arrays-- a 1D array that contains set of of categories, and a 2D array that contains an arbitrary number of arrays whose contents follow the category templates established by the first array. For instance:
var categoryArray = ["Name", "Title", "Hire Date"];
var infoArray = [["John","Project Manager","January 5"], ["Alex","Accountant","December 15"], ["Joanne","Graphic Designer","August 26"]];
What I'd like to do is consolidate this information into a single object. So up top I drop:
var myDict = {};
My (obviously wrong) attempt was to have nested for-loops which go through each array and attempts to fill the myDict object with the contents of the arrays. It looks like this:
// Start by iterating by the length of the info array, so we assign a new sub-object to the myDict Object for each entry.
for (i = 0; i < infoArray.length; i++) {
// Each entry will be a new "row," like in a spreadsheet.
var row = String("row"+i);
// I'm guessing that the declaration below doesn't actually assign the "Row1", "Row2", etc as nested objects like I had intended, but just re-writes a child called "row"
myDict.row = {};
// Next we iterate through the number of categories we'll need-- and we'll pull from the length of our categoryArray so we can change the number of categories later.
for (x = 0; x < categoryArray.length; x++) {
// In theory, the first iteration of this will create a child of "row1" called "name," which will hold a value of "John" (so, the value stored infoArray[1][1])
myDict.row.categoryArray[x] = infoArray[i][x];
}
}
There's clearly a LOT wrong in there, but I'm totally scratching my head about how to approach this. I guess the key problem is trying to assign the actual strings held by variables/arrays as names for objects/children, but a thorough googling has produced no answers.
Save me oh mighty internet!
var result = infoArray.map(function(currentArray) {
return categoryArray.reduce(function(previous, currentKey, index) {
previous[currentKey] = currentArray[index];
return previous;
}, {});
});
Output
[ { Name: 'John',
Title: 'Project Manager',
'Hire Date': 'January 5' },
{ Name: 'Alex',
Title: 'Accountant',
'Hire Date': 'December 15' },
{ Name: 'Joanne',
Title: 'Graphic Designer',
'Hire Date': 'August 26' } ]
If your environment doesn't support Array.prototype.map function, you can use this
var result = [];
for (var i = 0; i < infoArray.length; i += 1) {
var tempObj = {};
for (var j = 0; j < categoryArray.length; j += 1) {
tempObj[categoryArray[j]] = infoArray[i][j];
}
result.push(tempObj);
}
Using row0, row1, etc. doesn't sound like a good idea. An array would make more sense. Anyway:
var categoryArray = ["Name", "Title", "Hire Date"];
var infoArray = [["John","Project Manager","January 5"],["Alex","Accountant","December 15"],["Joanne","Graphic Designer","August 26"]];
var myDict = {};
for (i = 0; i < infoArray.length; i++) {
// Each entry will be a new "row," like in a spreadsheet.
var row = {};
myDict["row"+i] = row;
// Next we iterate through the number of categories we'll need-- and we'll pull from the length of our categoryArray so we can change the number of categories later.
for (x = 0; x < categoryArray.length; x++) {
// In theory, the first iteration of this will create a child of "row1" called "name," which will hold a value of "John" (so, the value stored infoArray[1][1])
row[categoryArray[x]] = infoArray[i][x];
}
}
Output:
{
"row0": {
"Name": "John",
"Title": "Project Manager",
"Hire Date": "January 5"
},
"row1": {
"Name": "Alex",
"Title": "Accountant",
"Hire Date": "December 15"
},
"row2": {
"Name": "Joanne",
"Title": "Graphic Designer",
"Hire Date": "August 26"
}
}
// I'm guessing that the declaration below doesn't actually assign the "Row1", "Row2", etc as nested objects like I had intended, but just re-writes a child called "row"
myDict.row = {};
Exactly. For that, you will have to use
myDict[row] = {};
(and the same again below: myDict[row][…] = …)
// In theory, the first iteration of this will create a child of "row1" called "name," which will hold a value of "John" (so, the value stored infoArray[1][1])
myDict.row.categoryArray[x] = infoArray[i][x];
Again .categoryArray[x] is here used as a literal property name (the property x of the property "categoryArray", which does not exist) - you will have to wrap it in brackets.
You should end up with:
var myDict = {};
for (var i = 0; i < infoArray.length; i++) { // make iteration variables local
// with "var"
var row = "row"+i; // explicit String() call not necessary
myDict[row] = {};
for (var x = 0; x < categoryArray.length; x++) { // again, local "x"
myDict[row][categoryArray[x]] = infoArray[i][x];
}
}
Related
I get issues when I want to loop through a JSON array of objects.
Issues such as:
It only counts two (I assume because of they Object.keys) and I have two keys.
Loops with only one value
My code:
var codes = require('./nl.json');
for (var i = 0, l = Object.keys(codes).length; i <= l; i++) {
console.log(l) ;
var areaCodeTest = codes.netherlands[i].areaCode;
var areaNameTest = codes.netherlands[i].areaName;
it("Search for postal code ", function(){
var postCode = element(by.id("imysearchstring"));
postCode.click();
browser.sleep(1000);
console.log(areaCodeTest);
postCode.clear().sendKeys(areaCodeTest);
browser.sleep(1000);
console.log("Typed " + areaCodeTest);
});
}
My Json (Short example):
{
"netherlands": [
{
"areaCode": 9401,
"areaName": "Assen"
},
{
"areaCode": 9402,
"areaName": "Assen"
},
{
"areaCode": 9403,
"areaName": "Assen"
}
]
}
I have looked at answers such as :
Size of Object and
Length of Json
I have tried:
(var i = 0, l = Object.keys(codes).length; i <= l; i++)
(var i = 0, l = Object.keys(codes.netherlands[0]).length; i <= l; i++)
for (var i = 0, l = codes.netherlands.length; i <= l; i++) // uses last areaCode in json file and only loop with that number. It does not start from top.
Image:
some of my outputs
Expected:
What I want is to count amount of ofjects in JSON (Not the key/values)
Loop through all data and assign them to var areaCodeTest = codes.netherlands[i].areaCode; and var areaNameTest = codes.netherlands[i].areaName;
I got it to work by using the following:
var codes = require('./nl.json');
codes.forEach((item) => {
var areaCodeTest = item.areaCode;
var areaNameTest = item.areaName;
it("and search for postal code ", function(){
var postCode = element(by.id("imysearchstring"));
postCode.click();
console.log(areaCodeTest);
postCode.clear().sendKeys(areaCodeTest);
browser.sleep(1000);
console.log("Typed " + areaCodeTest);
});
}
I am not a 100% what the => means near the foreach but I am currently researching why my code works. If you know please post a comment so that other developers also learn.
This let me think of the meme "not sure why code does not work / Not sure why code works"
You need to access the actual key in your loop in order to access codes[key]
Simplified version of your for() loop with stored variable for the object keys or using for in loop
const keys = Object.keys(codes)
for (let i = 0; i < keys.length; i++) {
// current object key and value of that property in main object
const key = keys[i], arr = codes[key];
console.log(`key = ${key}, length= ${arr.length}`)
// do another loop here for `arr` if needed
}
// OR using `for in`
for (let key in codes) {
console.log(`key = ${key}, length= ${codes[key].length}`)
}
<script>
const codes = {
"netherlands": [{
"areaCode": 9401,
"areaName": "Assen"
},
{
"areaCode": 9402,
"areaName": "Assen"
},
{
"areaCode": 9403,
"areaName": "Assen"
}
]
}
</script>
Try this I give you a sample
const object1 = {
a: 'somestring',
b: 42,
c: false
};
var length = (Object.keys(object1).length);
Please Refer this Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Hi I am trying to add some values to a multidimensional array using a for loop. This is what I have created so far:
var test1 = [];
var test2 = [];
for (var i = 0; i < top10.length; i++)
{
test1[i] = i;
test1[i][0] = top10[i][0];
test1[i][1] = top10[i][1];
}
This is just returning an empty array. top10 is a multidimensional array which contains:
It can contain more data that's why I need a for loop. I am trying to create 2 multidimensional arrays "test1" and "test2" one will contain "Hinckley Train Station" and "4754" the other will contain "Hinckley Train Station" and "2274".
I can have multiple venues not just "Hinckley Train Station" "4754" "2274" I could also have "London City" "5000" "1000". This is why it is a for loop.
You could push a new array to the wanted parts
var top10 = [
["Hinckley Train Station", "4754", "2274"],
["London City", "5000", "1000"]
],
test1 = [],
test2 = [],
i;
for (i = 0; i < top10.length; i++) {
test1.push([top10[i][0], top10[i][1]]);
test2.push([top10[i][0], top10[i][2]]);
}
console.log(test1);
console.log(test2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
In this line
test1[i] = i;
You are assigning an integer to be the first element of the outer array. You don't have a 2d array, you have an array of integers
In the following lines:
test1[i][0] = top10[i][0];
test1[i][1] = top10[i][1];
You are assigning properties to an integer, which means they are being boxed but the boxed value is thrown away.
It's hard to tell what you are trying to do, but the following is probably closer. You need to create a new inner array each time through the loop.
for (var i = 0; i < top10.length; i++)
{
test1[i] = [];
test1[i][0] = top10[i][0];
test1[i][1] = top10[i][1];
// Maybe do something similar with test2
}
So basically I want o loop through an error (what i already did) and print the other key
var planet = "sun";
var planets = [{
type: "sun",
desc: "sun"
}, {
type: "moon",
desc: "moon"
}];
var pln = planets.length;
for (var i = 0; i < pln; i++) {
console.log(planets[i]);
}
How can i I find out if the var planet is in the array planets and print out the description from that key if so?
Compare planet with current planets[i].type. Once planet is found in array remember to terminate loop using break keyword:
var pln = planets.length;
for (var i = 0; i < pln; i++) {
if (planets[i].type === planet) {
console.log(planets[i].desc);
break;
}
}
You could use filter
var planet = "sun";
var planets = [{
type: "sun",
desc: "sun"
}, {
type: "moon",
desc: "moon"}];
var planetObj = planets.filter(function(obj){
return obj.type==planet;
})[0].desc;//sun
If you are unsure if planet exists, you may wish to see if the array returned from filter actually contained any elements, or perhaps iterate the returned set if there were more than one you were searching for.
Hmm, I think this is what you want:
var pln = planets.length;
for (var i = 0; i < pln; i++) {
if (planets[i].type == planet) {
console.log(planets[i].desc);
break;
}
}
I get an object with partial results of match from database.
[Object { home1=4, away1=3, home2=4, away2=5, home3=6, away3=7, home4=6, away4=5, home5=3, away5=6}]
home1 it's a result of first part of home team,
away1 -> away team, home2 it's a result of second part of home team... etc etc
data in my case is each row, which i get from database.
In rows i have td with class: home1, home2, home3, away1, away2 and there are values of corresponding part of match.
I want to check if value is equal to what I got from database.
Something like this
if ($('.home1') === data[index].home1;
if($('.away2') === data[index].away2;
there should be some loop. I have no idea how to do this, I thought about an array
var array = [
{
home1: data[index].home1,
away1: data[index].away1
},
{
home2: data[index].home2,
away2: data[index].away2
},
{
home3: data[index].home3,
away3: data[index].away3
},
{
home4: data[index].home4,
away4: data[index].away4
},
{
home5: data[index].home5,
away5: data[index].away5
}
]
and then for loop:
for(var X=0; X<5;X++){
homeX == data[index].homeX
}
How can I increment name of variable by eval function? or is there any other solution? I'm very confused.
You can access object properties using operator []:
for(var i=0; i<array.length; i++)
{
var item = array[i];
var homePropertyName = 'home' + (i+1);
//now you can access homeX property of item using item[homePropertyName]
//e.g. item[homePropertyName] = data[index][homePropertyName]
}
Maybe you should use a little different structure which might fit your needs better, like this:
array = [
0: array [
"home": "Text for home",
"away": "Text for away"
],
1: array [
"home": "",
"away": ""
]
// More sub-arrays here
];
You can also initialize it with a for loop:
var array = new Array();
var i;
for (i = 0; i < 4; i++) {
array[i] = [
"home": "",
"away": ""
];
}
Or like this:
array[0]["home"] = "Text for home";
array[0]["away"] = "Text for away";
You can use this structure for the data-array also, and then use a for-loop to go through them both (like if you wish to find an element):
var result = NULL;
for (i = 0; i < array.length; i++) {
if ( (array[i]["home"] == data[index]["home"]) &&
(array[i]["away"] == data[index]["away"])
) {
// Found matching home and away
result = array[i];
break;
}
}
if (result != NULL) {
alert("Found match: " + result["home"] + " - " + result["away"]);
}
else {
alert("No match");
}
PS: Code is not tested, let me know if something is wrong.
you can access global properties in browser via window object like this (fiddle):
value1 = "ONE";
alert( window['value'+1] );
But it is not good design. You should look into how to properly format JSON object.
I have something like this:
for(var i=0; i<2; i++)
{
var item = ARR[i];
for(var x=0;x<5;x++){
var hPropertyName = 'home_p' + (x+1);
var aPropertyName = 'away_p' + (x+1);
item[hPropertyName] = ARR[i][hPropertyName];
item[aPropertyName] = ARR[i][aPropertyName];
}
and it works when i create an array:
var ARR = [
{
home_p1: 4,
away_p1: 5,
home_p2: 8,
away_p2: 9,
home_p3: 2,
away_p3: 1,
home_p4: 5,
away_p4: 3,
home_p5: 3,
away_p5: 2
},
{
home_p1: 6,
away_p1: 1,
home_p2: 1,
away_p2: 2,
home_p3: 3,
away_p3: 4,
home_p4: 5,
away_p4: 6,
home_p5: 3,
away_p5: 2
}
];
but I don't have to create an array, because i have to work on object which I get from database :
[Object { event_id=19328, home_result=3, away_result=2, home_p1=4, away_p1=3, home_p2=1, away_p2=2 ...... }]
I'm only interested in these parameters --> home_p , away_p
I want to push it to my array to looks like ARR. I think i should convert an object which I get to an array
If you are using string name for your attributes then you could try using template literals?
var someObject = {}
for(let i=0 ; i<values.length ; i++){
someObject[`home${i+1}`] = values[i];
}
and if you need it to be ES5 you could just use string concatenation. Below is a working example:
values = [1,2,3,4,5];
let someObject = {};
for(let i=0 ; i<values.length ; i++){
someObject[`value${i+1}`]=values[i];
}
console.log(someObject.value1);
console.log(someObject.value2);
console.log(someObject.value3);
console.log(someObject.value4);
console.log(someObject.value5);
Firstly, this is my json value i am getting from a php source:
[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]
After that, I want to get and oid value along with the corresponding cid value for example, oid=2 and cid=107 at one go, oid=4 and cid=98 at another and so on. I am trying to use jquery, ajax for this.
I have tried many answers for this, like: Javascript: Getting all existing keys in a JSON array and loop and get key/value pair for JSON array using jQuery but they don't solve my problem.
I tried this:
for (var i = 0; i < L; i++) {
var obj = res[i];
for (var j in obj) {
alert(j);
}
but all this did was to return the key name, which again did not work on being used.
So, you have an array of key/value pairs. Loop the array, at each index, log each pair:
var obj = [{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}];
for (var i = 0; i < obj.length; i++) {
console.log("PAIR " + i + ": " + obj[i].oid);
console.log("PAIR " + i + ": " + obj[i].cid);
}
Demo: http://jsfiddle.net/sTSX2/
This is an array that you have //lets call it a:
[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]
To get first element :
a[0] // this will give you first object i.e {"oid":"2","cid":"107"}
a[0]["oid"] // this will give you the value of the first object with the key "oid" i.e 2
and so on ...
Hope that helps.
`
Basically what you need is grouping of objects in the array with a property. Here I am giving two functions using which you can do this
// To map a property with other property in each object.
function mapProperties(array, property1, property2) {
var mapping = {};
for (var i = 0; i < data.length; i++) {
var item = data[i];
mapping[item[property1]] = mapping[item[property1]] || [];
mapping[item[property1]].push(item[property2]);
}
return mapping;
}
// To index the items based on one property.
function indexWithProperty(array, property) {
var indexing = {};
for (var i = 0; i < data.length; i++) {
var item = data[i];
indexing[item[property]] = indexing[item[property]] || [];
indexing[item[property]].push(item);
}
return indexing;
}
var data = [{
"oid": "2",
"cid": "107"
}, {
"oid": "4",
"cid": "98"
}, {
"oid": "4",
"cid": "99"
}];
var oidCidMapping = mapProperties(data, "oid", "cid");
console.log(oidCidMapping["2"]); // array of cids with oid "2"
var indexedByProperty = indexWithProperty(data, "oid");
console.log(indexedByProperty["4"]); // array of objects with cid "4"
May not be the exact solution you need, but I hope I am giving you the direction in which you have to proceed.
If you are willing to use other library you can achieve the same with underscorejs