How to pass an object property as a parameter? (JavaScript)

前端 未结 5 706
不知归路
不知归路 2020-12-24 01:54

I\'m not sure if the title is appropriate for what I am trying to achieve

I am mapping JSON results to an array. Because I need to do this over and over again I woul

相关标签:
5条回答
  • 2020-12-24 02:24

    Something like

    service.getData(function(data) {
        var map = {};
        var varNamePrefix = 'data';
        map = $.map(data, function(item, i) {
            return getProperties(item, varNamePrefix);
        });
    });
    
    service.getSomething(function(data) {
        var map = {};
        var varNamePrefix = 'something';
        map = $.map(data, function(item, i) {
            return getProperties(item, varNamePrefix);
        });
    });
    
    function getProperties(item, varNamePrefix) {
        var ret = {};
        ret.key = item[varNamePrefix + '1'];
        ret.value = item[varNamePrefix + '2'];
        return ret;
    }
    

    May help?

    Basically, you could use a function that takes a property prefix and uses it to form the actual property names to get from items.

    0 讨论(0)
  • 2020-12-24 02:28

    this can be done using the [] operators instead of the .-notation just like in this fiddle ive created just for you :D:

        var data = [{
            data1: 'foo',
            data2: 'bar',
            something1: 'sparky',
            something2: 'arf arf!'},
        {
            data1: 'My name is',
            data2: 'What?',
            something1: 'my name is',
            something2: 'Who?!'}
        ];
    
        function test(data, prop) {
            var d_length = data.length;
            for (var i = 0; i < d_length; i++) {
                alert(data[i][prop]);
            }
        }
    
        test(data, 'data1');
    
    0 讨论(0)
  • 2020-12-24 02:29

    You will need to pass the item object and a map of item-keys to entry-keys. This could either be two arrays, an array of tuples or something, or an object. Most simple method I could think of is to change the descriptor object into the entry itself:

    function mapPropertyNames(source, dest) {
        for (var prop in dest)
            dest[prop] = source[dest[prop]];
        return dest;
    }
    
    // and use it like this:
    
    var map = $.map(data, function(item, i) {
        return mapPropertyNames(item, {key:"something1", value:"something2"});
    });
    
    0 讨论(0)
  • 2020-12-24 02:33

    use [] with a string to pull out properties from objects dynamically.

    var a = { foo: 123 };
    a.foo    // 123
    a['foo'] // 123
    
    var str = 'foo';
    a[str]   // 123
    

    Which means we can refactor your method like so, just pass in the names of the properties you want to read as strings.

    var getKeyValueMap = function(data, keyPropName, valuePropName) {
      return $.map(data, function(item, i) {
        return {
          key:   item[keyPropName],
          value: item[valuePropName]
        }
      });
    };
    
    service.getData(function(data) {
      return getKeyValueMap(data, 'data1', 'data2');
    });
    
    service.getSomething(function(data) {
      return getKeyValueMap(data, 'something1', 'something2');
    });
    
    0 讨论(0)
  • 2020-12-24 02:42

    try this:

    service.getData(function(data, props) {// props is a property object
        var map = {};
        map = $.map(data, function(item, i) {
            var entry = {};
    
            for (var key in props) {
                entry[key] = item[key]
            }
    
            return entry;
        });
    });
    

    or use a property array

    service.getData(function(data, props) {// props is like ['data1', 'data2']
        var map = {};
        map = $.map(data, function(item, i) {
            var entry = {};
    
            for (var j = 0, k = props.length; j < k; j++) {
                entry[props[j]] = item[props[j]]
            }
    
            return entry;
        });
    });
    
    0 讨论(0)
提交回复
热议问题