Add space in Array Items


Add space in Array Items



I have an array with key value pair items I want to add space in Every Key after every capital letter.


var items = [
{
"BrandConstruct": 165,
"YearPlanData": "a"
},
{
"BrandConstruct": 236,
"YearPlanData": "c"
},
{
"BrandConstruct": 376,
"YearPlanData": "b"
}

]



Above is my array and I want it as below:-


var items = [
{
"Brand Construct": 165,
"Year Plan Data": "a"
},
{
"Brand Construct": 236,
"Year Plan Data": "c"
},
{
"Brand Construct": 376,
"Year Plan Data": "b"
}

]



and again I want to do it back in original form in javascript





What have you tried? What went wrong with your attempt? There's plenty of resources on similar issues from which you can get and apply solutions to yours. You only said what you want and SO is not a code writing service.
– Adriani6
Jun 28 at 12:54





Actually I am Using Handsontable plugin so I Use BrandConstruct, YearPlanData as My table headers. So i want to show them with space and when Save it again set to orignal form
– Anshul
Jun 28 at 13:00




1 Answer
1



You can do something like:


var result = items.map(o => { //Loop thru the array using map
return Object.entries(o).reduce((c, [k, v]) => { //Convert the object into an array using Object.entries | Use reduce to group the array
return Object.assign(c, { //Use Object.assign to create new object
[k.split(/(?=[A-Z])/).join(" ")]: v //Use regex to split the key by capital letter and join
})
}, {});
});



Here is a snippet (shorter version):




var items = [{"BrandConstruct":165,"YearPlanData":"a"},{"BrandConstruct":236,"YearPlanData":"c"},{"BrandConstruct":376,"YearPlanData":"b"}]
var result = items.map(o => Object.entries(o).reduce((c, [k, v]) => Object.assign(c, {[k.split(/(?=[A-Z])/).join(" ")]: v}), {}));

console.log(result);



You can use Object.keys() for Angular and IE9 compatible:


Object.keys()




var items = [{"BrandConstruct":165,"YearPlanData":"a"},{"BrandConstruct":236,"YearPlanData":"c"},{"BrandConstruct":376,"YearPlanData":"b"}];

var result = items.map(function(o) {
return Object.keys(o).reduce(function(c, k) {
c[k.split(/(?=[A-Z])/).join(" ")] = o[k];
return c;
}, {})
});

console.log(result);





Could you please tell me where to use above code on client side or server side?
– Anshul
Jun 28 at 13:07





It's Javascript (as your question is tagged with), so client side. Unless you're using Node.js, but you've made no mention of that
– Rory McCrossan
Jun 28 at 13:08






I am using javascript. but this code shows error in javascript at [k, v]
– Anshul
Jun 28 at 13:11





@Anshul What is the error? Where do use this? frontend?
– Eddie
Jun 28 at 13:12






I am using angular js1 simply
– Anshul
Jun 28 at 13:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages