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?

Share Improve this question asked Apr 2, 2017 at 15:10 SefinekSefinek 291 gold badge3 silver badges11 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 2
var 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