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

Create an object based on Array Javascript - Stack Overflow

programmeradmin2浏览0评论
let Array: any = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

How can I convert this array into an Object like below:

let Object: any = {
    time: {
        headerName: ""
    },
    monday: {
        headerName: "Monday"
    },
    tuesday: {
        headerName: "Tuesday"
    },
    wednesday: {
        headerName: "Wednesday"
    },
    thursday: {
        headerName: "Thursday"
    },
    friday: {
        headerName: "Friday"
    },
    saturday: {
        headerName: "Saturday"
    },
    sunday: {
        headerName: "Sunday"
    }
};

Any idea guys? Sorry am quiet new to JavaScript. How can I write a for loop so that i can achieve this result? Thanks in advance guys

let Array: any = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

How can I convert this array into an Object like below:

let Object: any = {
    time: {
        headerName: ""
    },
    monday: {
        headerName: "Monday"
    },
    tuesday: {
        headerName: "Tuesday"
    },
    wednesday: {
        headerName: "Wednesday"
    },
    thursday: {
        headerName: "Thursday"
    },
    friday: {
        headerName: "Friday"
    },
    saturday: {
        headerName: "Saturday"
    },
    sunday: {
        headerName: "Sunday"
    }
};

Any idea guys? Sorry am quiet new to JavaScript. How can I write a for loop so that i can achieve this result? Thanks in advance guys

Share Improve this question edited Apr 25, 2017 at 8:57 blackdaemon asked Apr 25, 2017 at 8:49 blackdaemonblackdaemon 7555 gold badges22 silver badges44 bronze badges 5
  • 2 Did you try anything? – Pritam Banerjee Commented Apr 25, 2017 at 8:51
  • Yeah tried with looping through the Array. But I am stuck on the Object part – blackdaemon Commented Apr 25, 2017 at 8:52
  • Was not sure how to continue. whether i declare the Object with my desired data Structure or not – blackdaemon Commented Apr 25, 2017 at 8:53
  • Because as per the requirement, I have two keys. Day and the headername. How to create the frame or datastructure first guys? – blackdaemon Commented Apr 25, 2017 at 8:55
  • Consider not naming your variables to Array and Object. You might have issues accessing globals with the same name in the scope of these variables – Balázs Édes Commented Apr 25, 2017 at 17:06
Add a ment  | 

4 Answers 4

Reset to default 5

var result = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"].reduce(function (acc, element) { 
     acc[element] = {};
     acc[element].headerName=element;
     return acc;
}, {time: ""});
console.log(result);

You can use reduce to convert to object in JavaScript.

any.reduce(function (acc, element) { 
     acc[element] = {};
     acc[element].headerName=element;
     return acc;
}, {time: ""})

simply try this

var any = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
var outputObj = { //create a new object with one default value
  time: {
    headerName: ""
  }
};
any.forEach(function(item) { //iterate the any array and then keep adding key and values to new Object
  outputObj[item] = {
    headerName: capitalizeFirstLetter(item)
  };
});

function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

console.log( outputObj );

var any = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];

var res ={};
res['time'] ={headername:""};
any.map(function(a){
res[a] = {headername:a}

})
console.log(res)

Using For loop.

function toObject(arr) {
  var rv = {};
  for (var i = 0; i < arr.length; ++i)
    if (arr[i] !== undefined) rv[arr[i]] = {"headerName":arr[i]};
  console.log(rv);
}
toObject(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]);

发布评论

评论列表(0)

  1. 暂无评论