I am new to angularjs so I dont know how to push any value to array at particular position. For example :
$scope.atparticular.push =
[
{questionText: 'queston1',questionId: '1',answerlst:[{answerText:'answer1',answerId: 'a1'},{answerText: 'answer2',answerId: 'a2'},{answerText: 'answer3',answerId: 'a3'}]},
{questionText: 'row2queston1',questionId: 'rq1',answerlst:[{answerText:'row2answer1',answerId: 'row2a1'},{answerText: 'row2answer2',answerId: 'row2a2'},{answerText: 'row2answer3',answerId: 'row2a3'}]}
];
If this is my object containing list and if I want to append answerlst with one more position containing empty values such as :
$scope.atparticular.push =
[
{questionText: 'queston1',questionId: '1',answerlst:[{answerText:'answer1',answerId: 'a1'},{answerText: 'answer2',answerId: 'a2'},{answerText: 'answer3',answerId: 'a3'},{answerText: '',answerId: ''}]},
{questionText: 'row2queston1',questionId: 'rq1',answerlst:[{answerText:'row2answer1',answerId: 'row2a1'},{answerText: 'row2answer2',answerId: 'row2a2'},{answerText: 'row2answer3',answerId: 'row2a3'},{answerText: '',answerId: ''}]}
];
Please give some suggestion. Thanks in advance.
If I understand correctly, you want to add an object at the end of the array answerlst of the first element of the array $scope.atparticular.push.
So you want:
$scope.atparticular.push[0].answerlst.push({answerText: '',answerId: ''});
Note that this has nothing to do with angularJS per se. It's just plain JavaScript.
Related
How can I spilt below Array and print verificationCode= "value" in consle.log
var backup = [{kind=admin#directory#verificationCode, etag="HIWxtAjmBmqPQjjDV2Duo181uSc/ojhx3q8-0EqFY6M1x0NOJ85j1xg", userId=116800256069112846055, verificationCode=37140335}]
You need to correct your JS object to be in this format here.
var backup = [
{kind:"admin#directory#verificationCode",
etag:"HIWxtAjmBmqPQjjDV2Duo181uSc/ojhx3q8-0EqFY6M1x0NOJ85j1xg",
userId:"116800256069112846055",
verificationCode:"37140335"}
]
and then you can use the following to access your values
backup[0].verificationCode;
I have the following array with data:
[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4"},
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4"},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4"}
]
All in my angular Cart scope:
var data = $scope.cart;
now i would like to add to each row the order number, so i use a push:
$scope.cart.push({"orderNumber": $scope.OrderNumber});
the problem is that is adds the order number but as a separate row and not inside each of the rows. so my desired output should look like this:
[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4","orderNumber":1 },
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4","orderNumber":1},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4","orderNumber":1}
]
how would i push to each row of the array? i tried several versions but can't seem to get it to work.
in order to achieve that you should iterate through each object and set order number separately
angular.forEach($scope.cart, function (value, key) {
value.orderNumber = $scope.OrderNumber;
});
You need to iterate over each item and add the orderNumber as a property for each item. So you can use forEach function of the angular for that.
angular.forEach($scope.cart, (item) => item.orderNumber = $scope.OrderNumber)
just got an answer that i implemented but the user has deleted the response, however it works perfect, this was the solution that i used:
$scope.cart.forEach(item => item.orderNumber = $scope.OrderNumber)
My required object key-value(property-value) Order : {three:3,two:2,one:1}
I want last added key at top,When i add key-value dynamically the order i got is given below,
var numObj={};
numObj["one"]=1;
numObj["two"]=2;
numObj["three"]=3;
console.log(numObj) // result i get is { one:1, three:3,two:2 }
Please any one help me to get this key-value order {three:3,two:2,one:1}
As the commenters point out, JavaScript objects have no defined order for iteration. However, JavaScript maps do: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map.
let aMap = new Map();
myMap.set('AKey1', 'AValue1');
myMap.set('AKey2', 'AValue2');
myMap.set('AKey3', 'AValue3');
for (let x of aMap) {
console.log(x[1]);
}
Will provide
AValue1
AValue2
AValue3
I want to add array as json value.
Json format is as follows.
json_data = [
'name':'Testing'
'email':'TestEmail'
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com']
]
How can I set value of 'links' in javascript like that?
I did as follows.
links_array = [];
links_array =['testing','test2'];
json_data.links = links_array;
I wanted to append these two string but couldn't.
Any help would be appreciate.
Assuming that the syntax of your example is correct, you can use the "push" method for arrays.
json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[]
};
json_data.links.push("test1#test.com");
json_data.links.push("test2#test.com");
json_data.links.push("test3#test.com");
You have to make little changes to make it work.
First thing, You have to replace initial square brackets with curly one. By doing this your object will become JSON Literal - a key value pair.
Second thing, You have missed commas after 'name':'Testing' and 'email':'TestEmail'
Below will work perfectly:
var json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com']
}
In addition to push as mentioned by #giovannilobitos you can use concat and do it all in one go.
var json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com'
]
};
var links_array = ['testing','test2'];
json_data.links = json_data.links.concat(links_array);
console.log(json_data.links);
On MDN's array reference you can find a more complete list of how to modify arrays in JavaScript.
I've got objects 'ing' with a field named 'id' and another one called 'fObj' with a field named 'contain'.
By using ng-repeat i'd like to show only these 'ing' objects where ing.id is a part of fObj.contain
e.g.
ing=[{id: 1,field: value},{id:2, field: othervalue},{id:3, field: cat}];
fObj={field1: value1, field: value2, contain: ':1:3:'};
By having this contain value I'd like to show only ing's with id=1 and id=3
Yeah, I know there are two types of data (number and string) but even if i changed numbers to strings it still didn't work
I just dont't know how to make it works. It's probably some kind of custom filter, but I've tried couples and nothing happend.
I would be glad if you suggest me a solution.
Thanks
In your controller,
var ids = fObj.contain.split(':');
// the array for your ng-repeat
var displayIng = [];
// loop the objects, see if the id exists in the list of id's
// retrieved from the split
for(i = 0; i < ing.length; i++) {
if(ids.indexOf(ing.id.toString()) displayIng.push(ing[i]);
}
I would split the numbers out of fObj.contain; and use them as hashmap object keys for simple filtering of the array
var ing=[{id: 1},{id:2},{id:3}];
var fObj={contain: ':1:3:'};
var IDs = fObj.contain.split(':').reduce(function(a,c){
a[c]=true;
return a;
},{});
// produces {1:true,3:true}
var filtered = ing.filter(function(item){
return IDs[item.id];
});
console.log(filtered)