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 }

17
  • 3
    What do you mean by "alias"? You cannot change n things without performing that operation n times. Commented Apr 24, 2017 at 3:50
  • 1
    Possible duplicate of Rename the property names and change the values of multiple objects Commented Apr 24, 2017 at 3:51
  • 2
    @guest271314: Sure, many people want that. Doesn't mean that it's possible/reasonable. Commented Apr 24, 2017 at 4:07
  • 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 Commented Apr 24, 2017 at 4: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? Commented Apr 24, 2017 at 4:08

2 Answers 2

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] }); 
Sign up to request clarification or add additional context in comments.

5 Comments

"value" property of object at index 1 should be 1
@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.)
My answers covers both scenarios :p forward thinking, that's me
@JaromandaX The return value is a string, the original input is a number.
easily fixed @guest271314 - don't you think - you're of course still assuming value is the ordinal position (I fixed it now)
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);

8 Comments

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 Yes, your approach is briefer.
@nnnnnn While at the topic of shorter, is it possible to achieve the requirement at stackoverflow.com/questions/43466657/… utilizing a single default parameter?
according to recent comment, the array length is not sure - so, hardcoding for array length 2 wont work
@JaromandaX The same pattern can extend for the .length of input array. That is, to meet requirement of "without looping".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.