self.resultList.forEach(function(item, index, enumerable){
console.log(self.resultList);
item.id=11;
item.get('id');
});
the item like this:
if item.id = 11;
the exception like this:
Assertion failed: You must use Ember.set() to access this property (of
[object Object])
so item.get('id') or item.set('id',11)
the exception like this
Uncaught TypeError: Object # has no method 'get'
is this item not the Ember's Object?so what the item is?
could someone tell me how to change the 'itme.id's value..
Thanks a million
You can use the Ember.set(yourObject, propertyName, value); and Ember.get(yourObject, propertyName); to safely set and get properties.
In your case:
self.resultList.forEach(function(item, index, enumerable) {
Ember.set(item, "id", 11);
Ember.get(item, "id");
});
In my case I did it in this way
//In my controller I've defined the array
displayInfoCheckboxes: [
{
turnover: {
label: "Turnover",
value: 1,
propertyName: "turnover"
}
}, {
pl: {
label: "P&L",
value: 1
}
}
]
//and in my handler I passed the full string path to the property in the set method
let displayInfoCheckboxes = this.get('displayInfoCheckboxes');
let controller = this;
displayInfoCheckboxes.forEach(function(items,index) {
for (var key in items) {
controller.set('displayInfoCheckboxes.' + index + '.' + key + '.value', false);
}
})
Related
I am not comfortable with certain subtleties, and here are 2 days that I go around in circles, to carry out "manipulations" of Objects in javascript (NodeJS), I therefore appeal to your knowledge!
I send elements from a json as a parameter in a .js script.
in this script, I would like to process the elements sent as a parameter (by a loop), to add them to a list, then to be able to add others "manually", to finally get a "list" of the set with different additional information.
my "test" script where I simulate the parameters received and "try" to get this "list":
let params = JSON.parse('{ "100": 3, "101": 1 }') // simulate parameters
let lstObj = {} // content all the list obj
// only for the test
function foo(type) {
return "type is " + type;
}
function addToList(id, type) {
let obj = {
id: id,
type: type,
test: foo(type)
}
console.log('from addToList() -> ', obj);
return obj;
}
// process the Obj from parameters
let index = 0;
for (let [key, value] of Object.entries(params)) {
console.log("from Param: ", `${key} -> ${value}`, " or ", key, "->", value);
obj = addToList(key, value); // seem work
//lstObj.key = obj; // use 'key' not the key value
//lstObj.[key] = obj; // error
//lstObj.`${key}` = obj; // error
//lstObj.["999"] = obj; // error
//index++; lstObj.index = obj; // bad :)
lstObj.a999 = obj; // Work ! but how can a make it ?
}
console.log('\nResult -> ', lstObj);
// Now want to manualy add other Obj in the List, like this ?
// lstObj.999 = addToList("999", 3)
I would like to get a result like this:
{
"100": {id: 100, type: 1, test: 'Type is 1', ....}
"102": {id: 102, type: 3, test: 'Type is 3', ....}
"110": {id: 110, type: 1, test: 'Type is 1', ....}
"305": {id: 305, type: 2, test: 'Type is 2', ....}
}
The purpose of being able to subsequently retrieve the object of an element by a call like: "lstobj.101"
Thank's a lot !
What you need is to assign the key to the object.
Change this line
lstObj.a999 = obj; // Work ! but how can a make it ?
to
lstObj[key] = obj;
What this does is assign whatever value is contained by variable key to be a key in variable lstObj, then assign the value of obj as it's value.
For example
let key = 'exampleKey';
let value = 'exampleValue';
let obj = {};
obj[key]=value; //now object is { 'exampleKey': 'exampleValue' }
I do not know why I got Uncaught SyntaxError for this code
const relationship2 = {
name = 'zero',
friends: ['nero', 'hero', 'xero'],
logFriends() {
this.friends.forEach(friend => {
console.log(this.name, frined);
});
},
};
Object needs key/value pair which are seprated by :not =
change this
name = 'zero',
to this
name : 'zero',
I have an user object - I want to generate test for each user property and check if it's the right type. However as typeof array is an object assertion fails on array properties with "AssertionError: expected [ 1 ] to be an object".
I have therefore checked if the property is an array and then generate special test for it. I'm wondering if this is the right approach? I have a feeling I'm misssing something obvious.
Object.keys(pureUser).forEach(property =>{
// since typeof array is an object we need to check this case separately or test will fail with expecting array to be an object
if (Array.isArray(pureUser[property])) {
it(`should have property ${property}, type: array`, function () {
user.should.have.property(property);
});
} else {
it(`should have property ${property}, type: ${(typeof pureUser[property])}`, function () {
user.should.have.property(property);
user[property].should.be.a(typeof pureUser[property]);
});
}
});
pureUser is something like this:
let pureUser = {
username: "JohnDoe123",
id: 1,
categories: [1,2,3,4]
}
User variable is defined elsewhere via got.js
change your test to be pureUser[property].should.be.an.Array or user[property].should.be.an.Array
forEach
The forEach() method calls a provided function once for each element in an array, in order.
let pureUser = {
username: "JohnDoe123",
id: 1,
categories: [1,2,3,4]
}
Object.keys(pureUser).forEach(property =>{
// since typeof array is an object we need to check this case separately or test will fail with expecting array to be an object
if (Array.isArray(pureUser[property])) {
console.log('Yes, it\'s an Array')
//it(`should have property ${property}, type: array`, function () {
// user.should.have.property(property);
//});
} else {
console.log('No, it\'s not an Array')
//it(`should have property ${property}, type: ${(typeof property)}`, function () {
//user.should.have.property(property);
// user[property].should.be.a(typeof pureUser[property]);
//});
}
});
When you use forEach on pureUser, the parameter will be the objects properties, like username, id, etc
let pureUser = {
username: "JohnDoe123",
id: 1,
categories: [1,2,3,4]
}
Object.keys(pureUser).forEach(property =>{
console.log(property);
});
You can also access the array in your forEach function.
arr.forEach(item, index, arr)
I am trying to create a generic function where you are able to pass an object that has a property of a random function. With this you should be able to set a property stating the key and value for each parameter of the function.
the generic function should then call this "random" function with all the parameters.
However im not quite sure how to do it?
// Keep in mind its created for demonstration purposes
var functionOne = function(id)
{
return id;
}
var functionTwo = function(id,name)
{
return id + ' 'name;
}
var functionThree = funciton(id,name,age)
{
return id + ' '+name+' '+age;
}
var obj = [
{
callback: functionOne,
callbackParameters: [{key: 'id', value: 1}]
},
{
callback: functionTwo,
callbackParameters: [{key: 'id', value: 1}, {key: 'name', value:'Marc'}]
},
{
callback: functionThree,
callbackParameters: [{key: 'id', value: 1}, {key: 'name', value: 'Marc'}, {key: 'age', value: 45}]
}
]
obj.forEach(function(x){
//How do i call it with the correct keys? :(
})
Fiddle
You can call apply() on a function in JS and pass an array of parameters into it. So you could use the following assuming your callbackParameters are always in the correct order.
obj.forEach(function(x){
var parameters = x.callbackParameters.map(function(p) { return p.value; });
console.log(x.callback.apply(this, parameters));
})
Updated fiddle https://jsfiddle.net/y6oh1078/1/
Edit: Further reading
If you are interested in more ways to manipulate functions in JS, the following article on currying is a good read - https://www.sitepoint.com/currying-in-functional-javascript/
You cannot do this. This would require something like reflection, i.e. forEach anonymous function should know the definition of every callback function and get names (and what is much more important - order) of its arguments.
However, you can do the following:
var functionOne = function (o) {
return o.id;
};
var functionTwo = function (o) {
return o.id + ' ' + o.name;
};
var functionThree = function (o) {
return o.id + ' ' + o.name + ' ' + o.age;
};
var obj = [{
callback : functionOne,
callbackParameters : [{
key : 'id',
value : 1
}
]
}, {
callback : functionTwo,
callbackParameters : [{
key : 'id',
value : 1
}, {
key : 'name',
value : 'Marc'
}
]
}, {
callback : functionThree,
callbackParameters : [{
key : 'id',
value : 1
}, {
key : 'name',
value : 'Marc'
}, {
key : 'age',
value : 45
}
]
}
];
// Now, are you able to generate `o` objects dynamically using something like:
obj.forEach(function (x) {
var o = {};
x.callbackParameters.forEach(function (p) {
o[p.key] = p.value;
});
console.log(x.callback(o));
});
P.S. Actually, you can dynamically get the names and order of function arguments, but you do not want to do this.
If you are still interested in this, read this question.
I have a array in my controller Which has assign multiple keys with the value i need to access these values in controller so that i can save these data to database
Array is given below:-
$scope.Notes['surfacedefault-1'] = { value: "xyz" };
$scope.Notes['surfacedefault-2'] = { value: "we" };
$scope.Notes['surfacedefault-3'] = { value: "123" };
$scope.Notes['surfacedefault-4'] = { value: "red" };
$scope.Notes['surfacedefault-5'] = { value: "blue" };
Please suggest me to access their value in controller using foreach loop
It should be like this
angular.forEach($scope.Notes, function(value, key) {
console.log(key + ': ' + value);
});
Also you can make use of .push to create key value pairs in array, eg:
$scope.Notes.push({'surfacedefault-1':'xyz'});
use forEach in angular
angular.forEach($scope.Notes, function (val, key) {
console.log(val.value)
})
DEMO
that could be a solution?
$scope.Notes=[
{name:'surfacedefault-1', value: "xyz" },
{name:'surfacedefault-2', value: "we" },
{name:'surfacedefault-3', value:"123" },
{name:'surfacedefault-4',value: "red" },
{name:'surfacedefault-5',value: "blue" }];
for(var i=0;i<$scope.notes.length;i++{
do something
}
Use this.
var array = Object.keys($scope.Notes).map(function(key, index, array){
return $scope.Notes[key].value
})