admin管理员组文章数量:1357268
I have an array like this
propertiesToCheck: [
{name: 'name', type: 'type', enabled: true},
{
name: 'name2',
type: 'type2',
templateUrl: 'something.html',
preferences: {
places: [place1, place2,...],
placesDict: ????
},
enabled: true
}
]
In the places
array there are objects with id, name, etc. Now I want to create a dictionary placesDict
that should look like {place1.id: 0, place2.id: 0, ...}
, so all values will be set to 0. How can I do that?
I have an array like this
propertiesToCheck: [
{name: 'name', type: 'type', enabled: true},
{
name: 'name2',
type: 'type2',
templateUrl: 'something.html',
preferences: {
places: [place1, place2,...],
placesDict: ????
},
enabled: true
}
]
In the places
array there are objects with id, name, etc. Now I want to create a dictionary placesDict
that should look like {place1.id: 0, place2.id: 0, ...}
, so all values will be set to 0. How can I do that?
- Normally, you use "id" to identify something. Why want you alls id to be 0? and should the pacesDict be really a field in each array element? – Meier Commented Apr 2, 2017 at 16:01
-
Maybe I wasn´t clear. I want a dictionary, where there is id or name of the place used as a key, and as default all values are set to 0. Later I want to let the user set the values if he will want to do it, otherwise it will remain 0. I don´t want ids to be 0, just the values of the dictionary. I will show it to the user so he can change the values, I think I know how to do it for known items, but
places
array is dynamic, so I can´t just write all of it in the code.. – Sefinek Commented Apr 2, 2017 at 16:15
3 Answers
Reset to default 2var placesDict = {};
propertiesToCheck[1].preferences.places.forEach(place => {
placesDict[place.id] = 0;
});
Same thing but using function definition instead of arrow function.
var placesDict = {};
propertiesToCheck[1].preferences.places.forEach(setPlace);
function setPlace (place) {
placesDict[place.id] = 0;
}
To have this functionality attached to a method of the preferences
object:
propertiesToCheck: [
{name: 'name', type: 'type', enabled: true},
{
name: 'name2',
type: 'type2',
templateUrl: 'something.html',
preferences: {
places: [place1, place2,...],
placesDict: () => {
newDict = {};
propertiesToCheck[1].preferences.places.forEach(place => newDict[place.id] = 0);
return newDict;
}
},
enabled: true
}
]
console.log(propertiesToCheck[1].preferences.placesDict()); // {place1.id: 0, place2.id: 0, ...}
Setting the property placesDict
to be equal to a function return value:
propertiesToCheck: [
{name: 'name', type: 'type', enabled: true},
{
name: 'name2',
type: 'type2',
templateUrl: 'something.html',
preferences: {
places: [place1, place2,...],
placesDict: createDict()
},
enabled: true
}
]
function createDict() {
var placesDict = {};
propertiesToCheck[1].preferences.places.forEach(place => {
placesDict[place.id] = 0;
});
}
I hope this helped.
Better option in modern JS (>2019) is to use. It requires to have an Array of tuples (Array of Arrays). In following call I am extending Array with the map function which constructs the "nested array".
> const x = ["a", "b", "3"];
> Object.fromEntries(x.map(i => [i, undefined]));|
{ '3': undefined, a: undefined, b: undefined }
You can use the spread operator with following result:
> x = [1,2,3]
[ 1, 2, 3 ]
> {...x}
{ '0': 1, '1': 2, '2': 3 }
In Ramda for instance:
var createDict = R.pipe(R.pluck('id'), R.map(R.pair(R.__, 0)), R.fromPairs);
var places = [{ id: 'place1id' }, { id: 'place2id' }];
var placesDict = createDict(places);
// {"place1id": 0, "place2id": 0}
本文标签: angularjsjavascript create dictionary from arrayStack Overflow
版权声明:本文标题:angularjs - javascript create dictionary from array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744062657a2584412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论