1

I tried to make it by myself, but I don't know if this is possible

function smth(){
  var temp = [];
  for(var i = arguments.length -1; i > 2; i-=1){
    var temp2 = [];
    temp.push(arguments[i]);
    temp2.push(temp);
    temp = temp2;
    console.log(temp);
  }
  // I need to get array in this form 
  var something = item['collections']['0']['name'];
}
smth('collection','0','name');

edit:

Okay, maybe I haven't given you enough information. I've got a JSON object, and I'm making a filter function, and I'd like to make it more reusable because now I have hard-coded item.collections[0].name, and sometimes I need use item.parameters.name, and I will use it few a more times

$scope.$watch(name, function (newValue, oldValue) {
  if (newValue !== oldValue) {
    $scope.productsChucks = myFilter(array, function(item) {
      //console.log(item['collections']['0']['name']);
      if (item.collections[0].name == $scope[compareWith]) {
        return item;
      }
    });
  }
});
poxip
  • 909
  • 8
  • 25

2 Answers2

1

I think you stated your question completely wrong, IMHO it's a typical XY problem https://meta.stackexchange.com/a/66378

Anyway, based on your edit, what I think you really want is to get some nested properties of an object using a string in form "item.parameters.name".

The simplest way to do this is to use some kind of a helper library, eg. lodash:

_.get(item, 'parameters.name') // returns item.parameters.name
_.get(item, 'collections[0].name') // returns item.collections[0].name

Using that your code will look similar to this:

// path is a string given as the parameter to the filter
if (_.get(item, path) === $scope[compareWith]) {
    return item;
}

Your function smth will now take only one argument:

smth('collection[0].name');

More information about lodash can be found here https://lodash.com/docs#get

If you think you don't need whole lodash you can implement this single function by yourself, take a look here https://stackoverflow.com/a/6491621/704894

Community
  • 1
  • 1
Michał Miszczyszyn
  • 11,835
  • 2
  • 35
  • 53
0

If you need to access it this way:

var something = item['collections']['0']['name'];

Then it's not an array, but actually an object accessed with index notation. You can do it this way:

function smth() {
    var temp = {};
    var root = temp;
    for (var i = 0; i < arguments.length; i++) {
        temp[arguments[i]] = {};
        temp = temp[arguments[i]];
    }
    return root;
}

console.log(JSON.stringify(smth('collection', '0', 'name'), null, 2));
Bruno S.
  • 178
  • 1
  • 5
  • okey but how to display this informations with this var something = JSON.stringify(smth('collection', '0', 'name'), null, 2); item[something]; return me undefined :/ but i know that item['collections']['0']['name'] it's defined and have value – Mateusz Stefański Jan 28 '16 at 18:09
  • @MateuszStefański What's `JSON.stringify` here for? Do you understand what your code does? – Michał Miszczyszyn Jan 28 '16 at 19:25