I have problem to push some value that assigned to be an array in the object
Here my code :
var hasil = [{ product: 'listBarang[i][0]',
shoppers: [],
leftOver: 'listBarang[i][2]',
totalProfit: 0
}]
What I think is using push method like below
hasil.shoppers.push('test')
But it give me an error like this
TypeError: Cannot read property 'push' of undefined
Is anyone know how to deal with this?
hasil is an array of objects, so if you want to maniputate on those objects, you need to access them directly i.e. with hasil[0]:
hasil[0].shoppers.push('test')
Related
How do I check if a JSON object have key are duplicate, just get distint the key and not use foreach function? Such as:
var objectData = {[value1: abc], [value1: abc], [value2: bcd]}
If a key doesn't exist, and I try to access it, will it return undefined? Or get the error?
If this is meant to be an array of JSON objects:
var objectData = [ {"value1": "abc"}, {"value1": "abc"}, {"value2": "bcd"} ];
Then your answer lies within the responses to this question: Remove duplicates from an array of objects in javascript or this one Remove duplicate objects from an array using javascript.
If your notation accurately reflects what is produced by your system (or your own coding), you may want to rework it a bit so it makes sense.
Hope that helps.
I have data in response so here rcsaAssessmentData.data.lobCheckListDTOs is array , riskAsesCklstSessionKey is property of array , How can i get this key using filter or any other option.Below console is printing all objects but i just want value for the riskAsesCklstSessionKey.
So far tried code...
array.js
var opChecklist = rcsaAssessmentData.data.lobCheckListDTOs.filter(function(obj){
return obj.riskAsesCklstSessionKey;
console.log("opcheclist...............",obj.riskAsesCklstSessionKey);
});
$scope.challengesDTO.addChlngToChklst= obj.riskAsesCklstSessionKey;
This is what I think you mean:
You have an array of objects, rcsaAssessmentData.data.lobCheckListDTOs. Each of the objects in the array has a property called riskAsesCklstSessionKey. You are trying to get an array of those keys. If so, try this:
var keys = rcsaAssessmentData.data.lobCheckListDTOs.map(function(a) {return a.riskAsesCklstSessionKey;});
I am trying my best to program an angular app. I have a problem pushing an empty array into an object.
I get an error:
TypeError: Cannot read property 'push' of undefined
I have an object called items which looks like this:
Object
{
"Name": "name",
"Description": "description"
}
I would like to push an empty array into the object that can contain another array. Something like this.
Object
{
"Name": "name",
"Description": "description",
"Related Items": {
Item1:{...},
Item2:{...},
...
}
}
My controller does this when it is called:
$scope.push = function () {
$scope.item.push({"Related Items":[]});
};
I know I must be getting mixed up with something simple about the JSON Objects and Arrays, but I can't seem to find a solution.
Thankyou!
Since item is an object, you can just set the Related Items property:
$scope.item["Related Items"] = [];
$scope.item["Related Items"].push({});
However, above it looks like Related Items is actually an object with key names Item1, etc. rather than an array.
$scope.item["Related Items"] = {};
$scope.item["Related Items"].Item1 = {};
Javascript's push function only works when you're pushing values to an array. It won't work if you try to push to an object, instead it will try to call the key of "push" which doesn't exist. That's why you're getting the error you're getting.
Make sure that $scope.item is an array ([] or new Array), and then push to it with the value you would like.
$scope.item = [];
$scope.push = function () {
$scope.item.push({"Related Items":[]});
};
Here's W3School's .push() method explanation.
The item object should be like
{
....
"Related Items":<"some value">
....
}
It should already have key.
I am trying to access some data from an object as follows:
var summaryChanges = {
dataToAdd:[
{name:[]},
{events:[]},
{emails:[]}
],
dataToRemove:[
{name:[]},
{events:[]},
{emails:[]}
]
}
i am trying to log the contents of the name property of data to add as follows:
console.log($(summaryChanges.dataToAdd.name)[0]);
however the console only logs undefined.
dataToAdd is an arrary not an object , so access it like
console.log(summaryChanges.dataToAdd[0].name[0])
You need to realize some things
$(summaryChanges.dataToAdd.name) you are creating a jQuery Object.
summaryChanges it's an object so you can do sumaryChanges.dataToAdd
dataToAdd it's an array, so for get a value you access it like this dataToAdd[index]
At the end you access it like this
console.log(summaryChanges.dataToAdd[index].name[index])
So I have a dictionary-like object and an array:
var colors = {"b":color(0, 0, 0)};
var ar=[["b","0","0"],["b","b","0"],["b","b","b"]];
Now, I would like to get the value from the dictionary using the array like so:
colors.ar[0][0]
Which should give me the color black. However, this gives me an error:
Cannot read property '0' of undefined
I believe this is because it's trying to access colors."b" instead of colors.b .
So how can I get the property from the dictionary by using a value from the array?
Thanks!
I get it. What you want is this:
colors[ar[0][0]]
Since ar[0][0] resolves to "b", colors[ar[0][0]] resolves to colors["b"].
If you use dot notation it will try to access colors.ar which is undefined.