0

In es6 i have an array like

[{id:0,name:"a"},{id:1,name:"b"}]

and i want to change its props Name without mapping or looping

 [{value:0,label:"a"},{value:1,label:"b"}]

can i use the alias like function ?or any others alternatives?

thanks at first;

alias in function example: function (id:label){ ...in this function label as id }

tao.weng
  • 37
  • 6
  • 3
    What do you mean by "alias"? You cannot change *n* things without performing that operation *n* times. – Felix Kling Apr 24 '17 at 03:50
  • 1
    Possible duplicate of [Rename the property names and change the values of multiple objects](http://stackoverflow.com/q/10819863/218196) – Felix Kling Apr 24 '17 at 03:51
  • alias is like syntax in blow function `function(id:value){ below this function use value as id..... }` – tao.weng Apr 24 '17 at 03:58
  • @FelixKling _"without looping"_ – guest271314 Apr 24 '17 at 04:05
  • @tao.weng Are you trying to set default parameters at a function call? – guest271314 Apr 24 '17 at 04:05
  • 2
    @guest271314: Sure, many people want that. Doesn't mean that it's possible/reasonable. – Felix Kling Apr 24 '17 at 04:07
  • @FelixKling The Answers at linked Question each perform a loop – guest271314 Apr 24 '17 at 04:08
  • 1
    "array like" ... do you know the length, is it always 2 (or some other fixed number) - if not, then you'll need to loop or use black majick – Jaromanda X Apr 24 '17 at 04:08
  • 1
    If you have constraints such as "without mapping or looping", then you also have to tell us more about your data structure. Will there always be at most two elements in the array? Will the objects always have only these two properties? – Felix Kling Apr 24 '17 at 04:08
  • @guest271314: Because it's the most reasonable thing to do. – Felix Kling Apr 24 '17 at 04:09
  • @FelixKling Perhaps reasonable, though explicitly exclusive of requirement at present Question. The original Question is clear and concise as to parameters of requirement. At least, so far. Though appears OP could be trying to set default parameters of a function, if not achieve the current explicit requirement of Question. – guest271314 Apr 24 '17 at 04:09
  • 1
    *"i want to change it to below without mapping or looping"* - Why? Why deliberately try to avoid the simplest and easiest technique? It's a one-liner with `.map()`. – nnnnnn Apr 24 '17 at 04:20
  • why would value be 1 for the second item? – Jaromanda X Apr 24 '17 at 04:23
  • @JaromandaX the array length is not sure – tao.weng Apr 24 '17 at 06:13
  • my answer should work for arbitrary length of the array – Jaromanda X Apr 24 '17 at 06:17
  • @FelixKling in my case ,i use a react component which propsType need a array [{label:'',value:''}],but always dataSource dose not format. – tao.weng Apr 24 '17 at 06:23
  • @nnnnnn in my opinion,the syntax of param alias in function can be use ,it can be reduce the looping waster – tao.weng Apr 24 '17 at 06:39

2 Answers2

0

You can use object destructuring, Array.prototype.shift()

let arr = [{id:0,name:"a"},{id:0,name:"b"}];

let res = [];

([{id:res[res.length], name:res[res.length]}
, {id:res[res.length], name:res[res.length]}] = arr);

arr = [{value:res.shift(), label:res.shift()}
      , {value:res.shift() + 1, label:res.shift()}];

console.log(arr);
guest271314
  • 1
  • 15
  • 104
  • 177
  • Isn't this unnecessarily complicated? If the code is only going to work for a fixed number of elements wouldn't it be both shorter and easier to understand to just do it in one line with `arr = [{value: arr[0].id, label: arr[0].name}, {value: arr[1].id, label: arr[1].name}]`? – nnnnnn Apr 24 '17 at 04:18
  • @nnnnnn Yes, your approach is briefer. – guest271314 Apr 24 '17 at 04:19
  • @nnnnnn While at the topic of shorter, is it possible to achieve the requirement at http://stackoverflow.com/questions/43466657/can-we-set-persistent-default-parameters-which-remain-set-until-explicitly-chang utilizing a single default parameter? – guest271314 Apr 24 '17 at 04:41
  • according to recent comment, `the array length is not sure` - so, hardcoding for array length 2 wont work – Jaromanda X Apr 24 '17 at 06:17
  • @JaromandaX The same pattern can extend for the `.length` of input array. That is, to meet requirement of "without looping". – guest271314 Apr 24 '17 at 06:29
  • arbitrary length? – Jaromanda X Apr 24 '17 at 06:31
  • Yes. `res.length` is equal to length of all values of plain objects within input array. `res.length` should not exceed `arr.length * N` where `N` are values of objects within `arr`. The values are retrieved in order from `res` to set values at new object, where `res.length` is `0` at conclusion of process. – guest271314 Apr 24 '17 at 06:34
  • sorry, i fount that my value is wrong .i just want to change the propTypes name without looping. – tao.weng Apr 24 '17 at 06:54
0

If the number of entries is variable, you could use Proxy

var thing = [{id:0,name:"a"},{id:0,name:"b"}]

var newThing = new Proxy(thing, {
    get: (target, name, receiver) => !isNaN(parseInt(name)) ? ({value:target[name].id, label:target[name].name}) : target[name]
});

console.log(newThing[0]); //Object { value: 0, label: "a" }

newThing.forEach(item => console.log(item));
//Object { value: 0, label: "a" }
//Object { value: 0, label: "b" }

if value is the ordinal position (as is implied by your example input/output)

var newThing = new Proxy(thing, {
    get: (target, name, receiver) => !isNaN(parseInt(name)) ? ({value:parseInt(name), label:target[name].name}) : target[name]
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • `"value"` property of object at index `1` should be `1` – guest271314 Apr 24 '17 at 04:34
  • @guest271314 - I feel fairly confident that there was a typo in the question's input data (should've been 0 & 1, not 0 & 0). The subject line does ask about changing the property names, not values. (Otherwise it is really just (even more of) a guess at what the OP wants to do.) – nnnnnn Apr 24 '17 at 04:37
  • 1
    My answers covers both scenarios :p forward thinking, that's me – Jaromanda X Apr 24 '17 at 04:38
  • @JaromandaX The return value is a string, the original input is a number. – guest271314 Apr 24 '17 at 04:44
  • 1
    easily fixed @guest271314 - don't you think - you're of course still assuming `value` is the *ordinal position* (I fixed it now) – Jaromanda X Apr 24 '17 at 04:56