This question already has answers here:
How do I iterate over a JSON structure? [duplicate]
(13 answers)
Iterate over Javascript object [duplicate]
(1 answer)
Closed 8 years ago.
var questions = {
game: {
question_1: {
question: "Question 1",
points: 7
},
question_2: {
question: "Question 2",
points: 5
}
}
};
New to programming...
How do I use a for-loop to iterate over my questions object to access questions.game.question_1.question? Or questions.game.question_[n].question
var answers = [];
for(var i=0; i <questions.game.length; i++) {
questions.game.question_[i].question.push(answers);
}
Like this:
for (var key in questions.game) {
if (questions.game.hasOwnProperty(key)) {
questions.game[key].question.push(answers);
}
}
On the other hand, you're going to run into some trouble when you try to push an array into a string. Did you have that backwards?
for (var key in questions.game) {
if (questions.game.hasOwnProperty(key)) {
answers.push(questions.game[key].question);
}
}
Related
This question already has answers here:
Appending the count to duplicates in a javascript string array
(3 answers)
Closed 3 years ago.
Input:-
Here i'm having array object in which natraj is repeating so i need to append some count like given example.
var aa= [
{name:'natraj'},
{name:'ajai'},
{name:'natraj'},
{name:'ajai'},
{name:'barath'},
{name:'ajai'},
{name:'barath'},
{name:'natraj'},
]
output:-
[
{name:'natraj'},
{name:'ajai'},
{name:'natraj_1'},
{name:'ajai_1'},
{name:'barath'},
{name:'ajai_2'},
{name:'barath_1'},
{name:'natraj_2'},
]
Keep a running count:
var aa = [{name:'natraj'},{name:'ajai'},{name:'natraj'},{name:'ajai'},{name:'barath'},{name:'ajai'},{name:'barath'},{name:'natraj'},]
let counts = {}
aa.forEach(o => {
if (counts[o.name]) {
o.name += `_${counts[o.name]++}`
} else {
counts[o.name] = 1
}
});
console.log(aa)
This question already has answers here:
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 7 years ago.
I have rewritten a version of each below. In my output I want it to log
[['motorbike', 0, vehicles],[['car', 1, vehicles]],[['plane', 2, vehicles]]]
However it currently logs the entire contents of the array rather than just the title. How can I get it to provide the the list name instead?
function each(collection, callback) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
callback(collection[i], i, collection);
}
} else {
return collection;
}
}
var vehicles = ['motorbike', 'car', 'plane'];
var iterationInputs = [];
each(vehicles, function(vehicle, index, list) {
iterationInputs.push([vehicle, index, list]);
});
console.log(iterationInputs);
You can't access variable names
I think you need to use hash tables ,, this link can help you http://www.mojavelinux.com/articles/javascript_hashes.html
This question already has answers here:
How to remove item from array by value? [duplicate]
(37 answers)
Closed 8 years ago.
My code looks like this. At the beginning I have defined nested arrays
var license= [{
packet: [{
pay: [],
channel: [],
fun: []
}]
}];
Add item work corect
function addPayChannelToObject(name) {
var paychannel = { name: name };
license[0].packet[0].pay.push(paychannel);
console.log(pay);
}
How can I delete an item by value?
function removeItemFromObject(name) {
//??
console.log(pay);
}
Try this way
function removeItemFromObject(name) {
var index = license[0].packet[0].pay.indexOf(name); // found index of value
if(index > -1){
license[0].packet[0].pay.splice(index, 1); //remove index
}
console.log(pay);
}
This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 9 years ago.
I have an Object. How to get number of nested objects in a?
a = {
firstNested: {
name: 'George'
}
secondNested: {
name: 'James'
}
}
I supposed to use .length which is usually used for arrays. What should I do in this case?
Yes, duplicate of above.. Just use:
var a = {1:2, 3:4},
count = 0,
item;
for(item in a) {
count++;
}
alert(count);
This question already has answers here:
Update/Create object properties dynamically
(3 answers)
Closed 8 years ago.
{
'$or': [
{ adv_id: 3 },
{ adv_id: 3 }
]
}
Anyone know how to construct this type of Object programmatically in Javascript?
Not sure what you mean exactly. You could just assign that to a variable:
var object = {
'$or': [
{ adv_id: 3 },
{ adv_id: 3 }
]
}
Or, step by step:
var object = {};
object.$or = [];
object.$or[0] = {};
object.$or[1] = {};
object.$or[0].adv_id = 3;
object.$or[1].adv_id = 3;