JavaScript Multi Dimentional Array - javascript

I have created a multidimensional array for a jobs feed like so:
var jobs = [
["JOB222" , "Painter"],
["JOB333" , "Teacher"],
["JOB444" , "Delivery Driver"],
];
I can access the array using the index number
alert( jobs[2][1] ); // Alerts Delivery Driver
If I set the reference number manually, I can loop through the array to find a match.
var viewingJobRef = "JOB333";
for (var i=0;i<jobs.length;i++) {
if (jobs[i][0] == viewingJobRef) {
alert(jobs[i][1]); // This will alert Teacher
}
}
So my question is, is it possible to access the array directly and not use a loop?
var viewingJobRef = "JOB333";
alert( jobs[viewingJobRef][1] ); // I want this to alert Teacher
Firefox error console says:
jobs[viewingJobRef]is undefined, how do I do it?

You want to use objects:
var jobs = {
"JOB222" : "Painter",
"JOB333" : "Teacher",
"JOB444" : "Delivery Driver"
};
Access like this :
var viewingJobRef = "JOB333";
alert( jobs[viewingJobRef] );
OR
alert( jobs["JOB333"] );
OR
alert( jobs.JOB333 );

You may use objects:
var jobs = {
"JOB222": "Painter",
"JOB333": "Teacher",
"JOB444": "Delivery Driver"
};
And loop with:
for ( var i in jobs ) {...}
Or access directly like:
alert( jobs.JOB333 );

Related

How to get values in Json Array and use each of them separately

I have a JSON and I Want to separate each field of that in array part and then I want to use of each field separately and put them in separate array.
for example I have two part in my array and I want to divide the first part too room 1 and second part in room 2 .
I have to send Json to my second page with format room1 and room 2. and I do not know how can I do this
my json is like this right now :
"rooms": [
{
"adultcount": "1",
"childcount": "1,1"
},
{
"adultcount": "1",
"childcountandage": "0 "
}
]
but I want to change it like this :
"rooms": [
{
"rooms1": {
"adultcount": "1",
"childcount": "1,1"
}
},
{
"rooms2": {
"adultcount": "2",
"childcount": "10,1"
}
}
]
then I need to use them.
how can I do this with jquery ?
there is no need to change the json code I just wrote the sample the new json to define better.
here is my code :
$( document ).ready(function() {
var research={"rooms":[{ "adultcount":"1","childcount":"1,1" },{ "adultcount":"1","childcountandage":"0 " }] }
var adultcount = research.rooms[0].adultcount;
var childcount = research.rooms[0].childcount;
});
Since you have an array that you want to make into an object and the property name seems to be the index inside the array, you can use a basic array.reduce:
var rooms = [
{ "adultcount":"1", "childcount":"1,1" },
{ "adultcount":"2", "childcount":"10,1" }
];
var roomsMap = rooms.reduce( function( map, room, index ) {
map[ 'room' + ( index + 1 ) ] = room;
return map;
}, {} );
var otherRoomsMap = rooms.map( function( room, index ) {
var wrapper = {};
wrapper[ 'room' + ( index + 1 ) ] = room;
return wrapper;
} );
console.log( roomsMap );
console.log( otherRoomsMap );
edit:
I have added the other example of keeping the array and just wrapping the objects isnide another object, but I have no idea what advantage that would give over the original array.
You can access your json array using loop
$.each(research, function (key, value) {
var adultcount = value.adultcount;
var childcount = value.childcount;
console.log("Adult count is:"+value.adultcount);
console.log("Child count is:"+value.childcount);
});
Try this:
var research={"rooms":[{ "adultcount":"1","childcount":"1,1" },{"adultcount":"1","childcountandage":"0 " }] };
var newResearch = {"rooms": []};
research.rooms.forEach(function(r) {
newResearch.rooms.push({"room1": r[0], "room2": r[1]});
});
console.log(newResearch);

Search array for string - returning -1

I've been reading lots of StackOverflow answers which tell me that, in Javascript, the best way to search an array for a particular string is use indexOf(). I have been trying to make this work for a while now, and I need some help with it.
I am making a shop in a text-adventure game. These are the values I am using:
The array shopCosts:
shopCosts = [20, 25];
The array shopItems:
shopItems = [["Sword", "Shield"]];
I dynamically create radiobuttons by looping through shopItems:
for(var i = 0; i < array.length; i++)
{
// Create the list item:
var item = document.createElement('li');
// Set its contents:
item.appendChild(document.createTextNode(array[i] + " - " + shopCosts[i] + " Gold"));
// Add it to the list:
list.appendChild(item);
var label = document.createElement("label");
var radio = document.createElement("input");
var text = document.createTextNode(array[i]);
radio.type = "radio";
radio.name = "shop";
radio.value = array[i];
radio.onclick = function () { addValue(this.getAttribute("value"), shopCosts, shopItems) }
label.appendChild(radio);
label.appendChild(text);
document.body.appendChild(label);
}
This is the part in question:
radio.onclick = function () { addValue(this.getAttribute("value"), shopCosts, shopItems) }
My logic was basically to assign values to each dynamically created radiobutton, and if one was pressed, get the value (so, the name of the item you wanted to buy) and then search shopItems for that particular string for the index value. Once I had that, I would look in the same "parallel" list shopCosts to find the price.
I used console.log() to see what variables were in play. When I clicked on the radio button, this function is called:
function addValue(nameOfItem, shopCosts, shopItems)
{
var positionOfShopItem = shopItems.indexOf(nameOfItem);
console.log(positionOfShopItem);
console..log(nameOfItem);
console.log(shopItems);
}
Surely, the console.log() would return the position of the named item? To prove to myself I'm not going crazy, here's what the Dev Tools say:
-1
Sword
[Array[2]]
0: "Sword"
1: "Shield"
Sword is clearly in the array, in position 0, so why is indexOf() returning -1?
Any help appreciated!
As I alluded to in my comment, its because shopItems does not contain an array of strings, it contains a single element, where that one element is an array of strings. I suspect your code would work just fine if you removed the extra square braces
var shopItems = ["Sword", "Shield"];
I realize you've already fixed the bug, but I urge you to consider a different approach to the problem. These two principles will not only solve the problem in a cleaner way, but they also give you a new way to think about similar problems in the future:
Never use parallel arrays. Use a single array of objects instead.
In your main loop that appends the items, put the main body of the loop in a function.
If you follow these two ideas you gain several benefits. The code becomes much more straightforward, easier to maintain, and you don't have to do any array lookups at all!
Each shop item is packaged up as a single object in the array, like this:
var shopItems = [
{ name: 'Sword', cost: 20 },
{ name: 'Shield', cost: 25 }
];
So if you have a reference to the shop item as a whole, say in a variable called shopItem, then you automatically have all of its properties available: shopItem.name and shopItem.cost. This lets you also easily add more bits of data to a shop item, e.g.
var shopItems = [
{ name: 'Sword', cost: 20, dangerous: true },
{ name: 'Shield', cost: 25, dangerous: false }
];
and now shopItem.dangerous will give you the appropriate value. All without any array lookups.
Making the main loop body into a function adds a further benefit: Inside that function, its parameters and local variables are preserved each time you call the function (this is called a closure). So now you don't even have to fetch the list item value and look it up - you already have the appropriate shopItem available in the code.
Putting this together, the code might look like this:
var shopItems = [
{ name: 'Sword', cost: 20, dangerous: true },
{ name: 'Shield', cost: 25, dangerous: false }
];
var list = document.getElementById( 'list' );
for( var i = 0; i < shopItems.length; ++i ) {
appendShopItem( shopItems[i] );
}
// Alternatively, you could use .forEach() instead of the for loop.
// This will work in all browsers except very old versions of IE:
// shopItems.forEach( appendShopItem );
function appendShopItem( shopItem ) {
// Create the list item:
var item = document.createElement( 'li' );
// Set its contents:
item.appendChild( document.createTextNode(
shopItem.name + ' - ' + shopItem.cost + ' Gold'
) );
// Add it to the list:
list.appendChild( item );
var label = document.createElement( 'label' );
var radio = document.createElement( 'input' );
var text = document.createTextNode( shopItem.name );
radio.type = 'radio';
radio.name = 'shop';
radio.value = shopItem.name;
radio.onclick = function () {
addValue( shopItem );
};
label.appendChild( radio );
label.appendChild( text );
document.body.appendChild( label );
}
function addValue( shopItem ) {
console.log( shopItem );
alert(
shopItem.name +
' costs ' + shopItem.cost + ' and is ' +
( shopItem.dangerous ? 'dangerous' : 'not dangerous' )
);
}
New fiddle (with a tip of the hat to Jamiec for the original fiddle)
As you can see, this makes the code much easier to understand. If you have a shopItem, you automatically have its name, cost, and any other property you want to add. And most importantly, you never have to keep track of putting your values in the same order in two, three, or even more different arrays.
shopItems is an Array of Arrays. The 0 index of shopItems contains another array which contains:
["Sword", "Shield"]
So when you are trying to find the "Sword" item or "Shield" Item inside of shopItems it is returning -1 because it cannot find either inside of the array.
Change
shopItems = [["Sword", "Shield"]];
To
shopItems = ["Sword", "Shield"];
And that will fix your issue.
I've fixed it!
Removing the double square brackets resulted in this mess. So, as a workaround, I simply added [0] to var positionOfShopItem = shopItems.indexOf(nameOfItem); to get var positionOfShopItem = shopItems[0].indexOf(nameOfItem);
Thanks for everyone's help.

JSON/JS- Getting JSON object name

I have the following object consisting of three other objects of type array :
I want to get the text samsung out of that object and the rest of the objects. I have tried using a for in loop but I am getting the keys 0, 1 , 2.
for(brand in brands){
console.log(brand) // prints out `0, 1 , 2`
}
so I added another nested for in loop, but than I get 0, 0, 0 which are the keys within samsung, and other array objects within the rest of the objects.
What am i missing ?
You can use Object.keys(brands[brand]), and, based on your example, you would want Object.keys(brands[brand])[0]
Your loop is looping through the entire array, rather than the keys of an object.
var brand = brands[0];
for ( var i in brand )
console.log( brand[i] ) // -> samsung
Or to get them all:
for ( var i = 0; i < brands.length; i++){
var brand = brands[i];
for ( var i in brand )
console.log( brand[i] ) // -> samsung
}
You can try
for(brand in brands[0]){
console.log(brand);
}
assuming the name of the array to be brands
EDIT
For all the elements in the array brands
brands.forEach(function(v){
for(brand in v){
console.log(brand);
}
});

JavaScript dynamic array with object and array

I have a problem with creating list with objects in JavaScript: the object is going to contain an Integer value and an array.
The structure will look like this (this code works):
var list = [ {
id : 1234,
dependencyList : [ {
id : 2142
}, {
id : 5313
} ]
}, {
id : 4312,
dependencyList : [ {
id : 2142
}, {
id : 5313
} ]
} ];
The problem is that I want to create this list dynamically, in other words something like this:
var list = [];
var object;
object.id=1234;
object.dependencyList = [];
object.dependencyList.push(2222);
list.push(object);
This code does not work when I try to alert(list[0].length) and it returns 0 and after using JSON.stringify(list) it only returns [[]].
Anyone know a solution for this?
list[0] is an object and not an array, which is why you're not seeing anything for length. Try:
console.log(Object.keys(list[0]).length);
Or you could even just console.log(list[0]) and check your console to ensure that it contains something.
Also, I assume you meant:
var object = {};
Because otherwise object is undefined and your script won't work. Another thing you will have to change is:
object.dependencyList.push(2222);
to:
object.dependencyList.push({id: 2222});
Object isn't defined. Do:
var object = {};
You should do list[0].dependencyList.length) also you shoud change:
object.dependencyList.push(2222);
for
object.dependencyList.push({id: 2222});
Objects are your friend.
If you need to create a lot of these things, you should probably define how they are shaped in a function. An advantage of this is that you can then create a setter for adding a dependency that hides the implementation as a push against a particular member variable.
Something like this:
function createObject( id ) {
var object = {};
object.id = id;
return object;
}
function createObjectWithDependencies( id ) {
var object = createObject( id );
object.dependencyList = [];
object.addDependency = function( dependentObject ) {
object.dependencyList.push( dependentObject );
}
return object;
}
var list = [];
var object = createObjectWithDependencies( 1234 );
object.addDependency( createObject( 2142 ) );
object.addDependency( createObject( 5313 ) );
list.push( object );
var object = createObjectWithDependencies( 4312 );
object.addDependency( createObject( 2142 ) );
object.addDependency( createObject( 5313 ) );
list.push( object );
console.log( list );

NodeJS - how to send a variable to nested callbacks? (MongoDB find queries)

I want to use result set of a find query in another result set. I couldn't explain this situation in english very well. I will try to use some code.
People.find( { name: 'John'}, function( error, allJohns ){
for( var i in allJohns ){
var currentJohn = allJohns[i];
Animals.find( { name: allJohns[i].petName }, allJohnsPets ){
var t = 1;
for( var j in allJohnsPets ){
console.log( "PET NUMBER ", t, " = " currentJohn.name, currentJohn.surname, allJohnsPets[j].name );
t++;
}
}
}
});
Firstly, I get all people with find who are named John. Then I take those people as allJohns.
Secondly, I get all pets of every Johns one by one in different find queries.
In second callback, I get every pet one by one again. But when I want to display which John is their owners, I always got same John.
So, the question is: how can I send every John separately to the second nested callback and they will be together as real owners and pets.
I need to copy every John but I have no idea how can I do this.
Javascript has no block scope, only function scope. Instead of for .. in .., using forEach will create a new scope for each loop:
People.find( { name: 'John'}, function( error, allJohns ){
allJohns.forEach(function(currentJohn) {
Animals.find( { name: currentJohn.petName }, function(err, allJohnsPets) {
allJohnsPets.forEach(function(pet, t) {
console.log( "PET NUMBER ", t + 1, " = ", currentJohn.name, currentJohn.surname, pet.name );
});
});
});
});
You have to give more concentration on asynchronous nature.
People.find( { name: 'John'}, function( error, allJohns ){
for( var i=0; i<allJohns.length; i++ ){
(function(currJohn){
var currentJohn = currJohn;
Animals.find( { name: currentJohn.petName }, function(error, allJohnsPets){
for(var j=0; j<allJohnsPets.length; j++){
console.log( "PET NUMBER ", (j+1), " = " currentJohn.name, currentJohn.surname, allJohnsPets[j].name );
}
})
})(allJohns[i]);
}
});

Categories

Resources