最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to convert array of numbers to dictionary - Stack Overflow

programmeradmin4浏览0评论

I am trying the following code:

dict=deliveryDays.split(";").map((item,i)=>{
    return ({ i :item})
});

deliveryDays is an array of numbers but i inside the { i :item} is not the i passing in the map. what I want is:

[{0:"1", 1:"1", 2:"1" }]

but what I see is:

[{"i":"0"},{"i":"1"},{"i":"1"}]

So as you can see the i does not get the right value of i. How can I achieve this?

I am trying the following code:

dict=deliveryDays.split(";").map((item,i)=>{
    return ({ i :item})
});

deliveryDays is an array of numbers but i inside the { i :item} is not the i passing in the map. what I want is:

[{0:"1", 1:"1", 2:"1" }]

but what I see is:

[{"i":"0"},{"i":"1"},{"i":"1"}]

So as you can see the i does not get the right value of i. How can I achieve this?

Share Improve this question edited Oct 24, 2019 at 8:27 Yosvel Quintero 19.1k5 gold badges39 silver badges47 bronze badges asked Mar 6, 2019 at 3:41 Hamed MinaeeHamed Minaee 2,5604 gold badges40 silver badges68 bronze badges 1
  • What is the input? – Maheer Ali Commented Mar 6, 2019 at 3:52
Add a ment  | 

5 Answers 5

Reset to default 4

You can use spread syntax to spread your array inside a new object. I don't see the point of have an array including only one object based on your expected output: [{0:"1", 1:"1", 2:"1" }]. So my first guess will be that you need this::

let deliveryDays = "1;2;3;4;5"
let obj = {...deliveryDays.split(";")};
console.log(obj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

If you still need the only object inside an array, then is easy to figure out that next code will do it:

let obj = [{...deliveryDays.split(";")}];

In the other hand, if you want an array with multiple objects, then you can do this:

let deliveryDays = "1;2;3;4;5"
let res = deliveryDays.split(";").map((x, idx) => ({[idx]: x}));
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Use the [] bracket notation to evaluate the value of i. So in your case it is not evaluated but simply outputs "i" as a string in the key of the object.

const deliveryDays = "1;2;3;4";
const dict = deliveryDays.split(";").map((item,i)=> ({[i]:item}));
console.log(dict);

But I believe you want it as a single object wrapped in an array which can be done using Array.reduce:

const deliveryDays = "1;2;3;4";
const dict = deliveryDays.split(";").reduce((acc,item,i)=>{
  acc[i] = item;
  return acc;
},{});
console.log([dict]);

You can use Object.assign():

const deliveryDays = '1;2;3;4;5;6';
const dict = [Object.assign({}, deliveryDays.split(';'))];

console.log(dict);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can use Spread Syntax

const arr = [0, 1, 1];
let result = [{...arr}]
console.log(result);

One more way is to use Object.entries

const deliveryDays = [1, 2, 3, 4, 5, 6];
const dict = Object.entries(deliveryDays).map(([key,value])=>({[key]:value}))

console.log(dict);
.as-console-wrapper { max-height: 100% !important; top: 0; }

发布评论

评论列表(0)

  1. 暂无评论