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

前端 未结 5 760
不知归路
不知归路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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"});
    });
    

提交回复
热议问题