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;
Related
This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 3 years ago.
I send a get request to the api wichh returns data under form of
{
"message": "<SUCCESS>",
"result": [
{
"i": 1,
},
{
"i": 2,
},
{
"i": 3,
} ]
}
Then in javascript (angular component.ts) I want to create an array ob objects but after pushing the objects in the array every array contains the data of the last object:
.subscribe(
data => {
type MyArrayType = Array<UserOpinion>;
let array: MyArrayType = [];
let cont: CustomInterface = {
i: ''
}
data.result.forEach(function(entry) {
cont.i = entry["i"];
array.push(cont);
console.log(cont);
//right value is shown
})
//console.log(array)
//array contains the connect number of objects but the objects are set on the same value: "i" = 3
Any ideas why this happens? Thank you
This should do it:
.subscribe(
data => {
type MyArrayType = Array<UserOpinion>;
let array: MyArrayType = [];
data.result.forEach(function(entry) {
array.push(entry);
});
}
);
You dont need cont just for copying the current object to another, you can use the current loop element directly.
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:
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);
}
}
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);