How to filter multidimensional JavaScript array - javascript

I have this data:
var object = [{
"nid": "31",
"0": {
"tid": "20",
"name": "Bench Press",
"objectDate": "2012-02-08",
"goal": "rep",
"result": "55.00",
"comments": "sick!",
"maxload": "250"
},
"1": {
"tid": "22",
"name": "Back Squat",
"objectDate": "2012-02-08",
"goal": "time",
"result": "8.00",
"comments": "i was tired.",
"maxload": "310"
}},
{
"nid": "30",
"0": {
"tid": "19",
"name": "Fran",
"objectDate": "2012-02-07",
"goal": "time",
"result": "5.00",
"comments": null
}}];
and I would like to filter it by name. For instance, if I apply a filter for the name "Fran", I'd like to have something like this:
[0] => Array
(
[tid] => 19
[name] => Fran
[objectDate] => 2012-02-07
[goal] => time
[result] => 5.00
[comments] =>
)
[1] => Array
(
[tid] => 19
[name] => Fran
[objectDate] => 2012-02-08
[goal] => rep
[result] => 55.00
[comments] => woohoo!
)
Is it possible to achieve?

The question is about multidimensional arrays. If you like me missed that here are solutions for normal arrays...
2020
filteredArray = array.filter(item => item.name.indexOf('Fran') > -1);
or
filteredArray = array.filter(function(item)
{
return item.name.indexOf('Fran') > -1);
}
2012 version
var result = [];
for (var i = 0; i < array.length; i++)
{
if (array[i].name === 'Fran')
{
result.push(array[i]);
}
}

There is no function for this in Javascript. You have to write your own function like this.
var arr = [{"nid":"31","0":{"tid":"20","name":"Bench Press","objectDate":"2012-02-08","goal":"rep","result":"55.00","comments":"sick!","maxload":"250"},"1":{"tid":"22","name":"Back Squat","objectDate":"2012-02-08","goal":"time","result":"8.00","comments":"i was tired.","maxload":"310"}},{"nid":"30","0":{"tid":"19","name":"Fran","objectDate":"2012-02-07","goal":"time","result":"5.00","comments":null}}];
function filterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var obj = array[i];
for(var key in obj){
if(typeof(obj[key] == "object")){
var item = obj[key];
if(item[prop] == value){
filtered.push(item);
}
}
}
}
return filtered;
}
var byName = filterByProperty(arr, "name", "Fran");
var byGoal = filterByProperty(arr, "goal", "time");

I would create a function for filtering :
function filter(array, key, value){
var i, j, hash = [], item;
for(i = 0, j = array.length; i<j; i++){
item = array[i];
if(typeof item[key] !== "undefined" && item[key] === value){
hash.push(item);
}
}
return hash;
}
A more robust solution might be adding a filter method to the prototype:
`This prototype is provided by the Mozilla foundation and
is distributed under the MIT license.
http://www.ibiblio.org/pub/Linux/LICENSES/mit.license`
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
Then simply call:
function filterName (item, index, array) {
return (item.name === "Fran");
}
var result = object.filter(filterName);

If your object contains 3 fields, say email, firstName, lastName then you use this.
ArrayObject
.filter(em => em.email.indexOf(searchEmail) > -1)
.filter(fn => fn.firstName.indexOf(searchFirstName) > -1)
.filter(ln => ln.lastName.indexOf(searchLastName) > -1)

The big trick is to make a flat array with just the item you want in it.
say you have a 2d array like so:
e = [[1,2],[1,2],[2,3],[4,5],[6,7]]
you make a new array:
var f = []
fill it with just 1 item:
for(var x=0;x<e.length;x++) f[x] = e[x][1]
giving us the likes of:
f = [2,2,3,5,7]
and then....
var g = e.filter( function(elem, pos){ return (f.indexOf(elem[1]) == pos) })
cowabunga!

I try the solution from Diode. I adapt for my case. There are 3 arrays included.
var arr =
[
{
taskTypeGroupId: "EXP.CONT", taskTypeGroupName: "Contradictoire",
taskType:
{
taskTypeId: "DGE-EXPCONT", taskTypeName: "Dégats des eaux contradictoire", defaultDuration: 60, isInProject: false,
dataItems:
{
id: "EXTRAFILLER5", label: "Divers 5"
}
}
},
{
takTypeGroupId: "EXPQUAL", taskTypeGroupName: "Contrôle qualité",
taskType:
{
taskTypeId: "DGE-EXPQUAL", taskTypeName: "Contrôle qualité dégats des eaux", defaultDuration: 60, isInProject: false,
dataItems:
{
id: "EXTRAFILLER5", label: "Divers 5"
}
}
}
];
function filterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var array1 = array[i];
for(var key in array1){
if(typeof(array1[key] == "object")){
var array2 = array1[key];
for (var key2 in array2){
if(typeof(array2[key2] == "object")){
var array3 = array2[key2];
if(array3[prop] == value){
filtered.push(array3);
}
}
}
}
}
}
return filtered;
}
JsBin example

Related

How can I use data deep inside Json? [duplicate]

I'm trying to build a function that would expand an object like :
{
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
}
Into a nested object :
{ab: {cd: {e:'foo', f:'bar'}, g:'foo2'}}
Like this php function : Set::expand()
Without using eval of course.
I believe this is what you're after:
function deepen(obj) {
const result = {};
// For each object path (property key) in the object
for (const objectPath in obj) {
// Split path into component parts
const parts = objectPath.split('.');
// Create sub-objects along path as needed
let target = result;
while (parts.length > 1) {
const part = parts.shift();
target = target[part] = target[part] || {};
}
// Set value at end of path
target[parts[0]] = obj[objectPath]
}
return result;
}
// For example ...
console.log(deepen({
'ab.cd.e': 'foo',
'ab.cd.f': 'bar',
'ab.g': 'foo2'
}));
If you're using Node.js (e.g. - if not cut and paste out of our module), try this package: https://www.npmjs.org/package/dataobject-parser
Built a module that does the forward/reverse operations:
https://github.com/Gigzolo/dataobject-parser
It's designed as a self managed object right now. Used by instantiating an instance of DataObjectParser.
var structured = DataObjectParser.transpose({
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
});
structured.data() returns your nested object:
{ab: {cd: {e:'foo', f:'bar'}, g:'foo2'}}
So here's a working example in JSFiddle:
http://jsfiddle.net/H8Cqx/
Function name is terrible and the code was quickly made, but it should work. Note that this modifies the original object, I am not sure if you wanted to create a new object that is expanded version of the old one.
(function(){
function parseDotNotation( str, val, obj ){
var currentObj = obj,
keys = str.split("."), i, l = keys.length - 1, key;
for( i = 0; i < l; ++i ) {
key = keys[i];
currentObj[key] = currentObj[key] || {};
currentObj = currentObj[key];
}
currentObj[keys[i]] = val;
delete obj[str];
}
Object.expand = function( obj ) {
for( var key in obj ) {
parseDotNotation( key, obj[key], obj );
}
return obj;
};
})();
var expanded = Object.expand({
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
});
JSON.stringify( expanded );
//"{"ab":{"cd":{"e":"foo","f":"bar"},"g":"foo2"}}"
Derived from Esailija's answer, with fixes to support multiple top-level keys.
(function () {
function parseDotNotation(str, val, obj) {
var currentObj = obj,
keys = str.split("."),
i, l = Math.max(1, keys.length - 1),
key;
for (i = 0; i < l; ++i) {
key = keys[i];
currentObj[key] = currentObj[key] || {};
currentObj = currentObj[key];
}
currentObj[keys[i]] = val;
delete obj[str];
}
Object.expand = function (obj) {
for (var key in obj) {
if (key.indexOf(".") !== -1)
{
parseDotNotation(key, obj[key], obj);
}
}
return obj;
};
})();
var obj = {
"pizza": "that",
"this.other": "that",
"alphabets": [1, 2, 3, 4],
"this.thing.that": "this"
}
Outputs:
{
"pizza": "that",
"alphabets": [
1,
2,
3,
4
],
"this": {
"other": "that",
"thing": {
"that": "this"
}
}
}
Fiddle
You could split the key string as path and reduce it for assigning the value by using a default object for unvisited levels.
function setValue(object, path, value) {
var keys = path.split('.'),
last = keys.pop();
keys.reduce((o, k) => o[k] = o[k] || {}, object)[last] = value;
return object;
}
var source = { 'ab.cd.e': 'foo', 'ab.cd.f': 'bar', 'ab.g': 'foo2' },
target = Object
.entries(source)
.reduce((o, [k, v]) => setValue(o, k, v), {});
console.log(target);
You need to convert each string key into object. Using following function you can get desire result.
function convertIntoJSON(obj) {
var o = {}, j, d;
for (var m in obj) {
d = m.split(".");
var startOfObj = o;
for (j = 0; j < d.length ; j += 1) {
if (j == d.length - 1) {
startOfObj[d[j]] = obj[m];
}
else {
startOfObj[d[j]] = startOfObj[d[j]] || {};
startOfObj = startOfObj[d[j]];
}
}
}
return o;
}
Now call this function
var aa = {
'ab.cd.e': 'foo',
'ab.cd.f': 'bar',
'ab.g': 'foo2'
};
var desiredObj = convertIntoJSON(aa);
Something that works, but is probably not the most efficient way to do so (also relies on ECMA 5 Object.keys() method, but that can be easily replaced.
var input = {
'ab.cd.e': 'foo',
'ab.cd.f': 'bar',
'ab.g': 'foo2'
};
function createObjects(parent, chainArray, value) {
if (chainArray.length == 1) {
parent[chainArray[0]] = value;
return parent;
}
else {
parent[chainArray[0]] = parent[chainArray[0]] || {};
return createObjects(parent[chainArray[0]], chainArray.slice(1, chainArray.length), value);
}
}
var keys = Object.keys(input);
var result = {};
for(var i = 0, l = keys.length; i < l; i++)
{
createObjects(result, keys[i].split('.'), input[keys[i]]);
}
JSFiddle is here.
Here is how I do this in one of my applications:
const obj = {
"start.headline": "1 headline",
"start.subHeadline": "subHeadline",
"start.accordion.headline": "2 headline",
"start.accordion.sections.0.content": "content 0",
"start.accordion.sections.0.iconName": "icon 0",
"start.accordion.sections.1.headline": "headline 1",
"start.accordion.sections.1.content": "content 1",
"start.accordion.sections.1.iconName": "icon 1",
"start.accordion.sections.2.headline": "headline 2",
"start.accordion.sections.2.content": "content 2",
"start.accordion.sections.2.iconName": "icon 2",
"end.field": "add headline",
"end.button": "add button",
"end.msgs.success": "success msg",
"end.msgs.error": "error msg",
};
const res = Object.keys(obj).reduce((res, key) => {
const path = key.split('.');
const lastIndex = path.length - 1;
path.reduce(
(acc, k, i, a) => acc[k] = lastIndex === i ?
obj[key] :
acc[k] || (/\d/.test(a[i+1]) ? [] : {}),
res
);
return res;
}, {});
console.log(res);
This is the answer as provided by #broofa, but converted to TypeScript.
type NestedObject = { [key: string]: any };
function objectify(obj: NestedObject): NestedObject {
const result: NestedObject = {};
for (const key in obj) {
let target: NestedObject = result;
const parts = key.split(".");
for (let j = 0; j < parts.length - 1; j++) {
const part = parts[j];
target = target[part] = target[part] || {};
}
target[parts[parts.length - 1]] = obj[key];
}
return result;
}

How to split a string to multiple arrays based on multiple separators in javascript

I have seen the questions but none of them helped me.
I have a string like this:
var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
I want that string to be in different arrays based on the separators "HU", "EN", "DE", "RO".
My approach currently is this(Working but not too elegant):
var Lang_Array1 = Lang_Array.split(",");
console.log(typeof(Lang_Array));
console.log(Lang_Array);
var HU_Langs = [];
var EN_Langs = [];
var DE_Langs = [];
var RO_Langs = [];
for(var i = 0; i < Lang_Array1.length;i++){
if(Lang_Array1[i] != "EN"){
if(Lang_Array1[i] != ""){
HU_Langs[i] = Lang_Array1[i];
}
}else{
for(i;i < Lang_Array1.length;i++){
if(Lang_Array1[i] != "DE"){
if(Lang_Array1[i] != ""){
EN_Langs[i] = Lang_Array1[i];
}
}else{
for(i;i < Lang_Array1.length;i++){
if(Lang_Array1[i] != "RO"){
if(Lang_Array1[i] != ""){
DE_Langs[i] = Lang_Array1[i];
}
}else{
for(i;i < Lang_Array1.length;i++){
if(Lang_Array1[i] != ""){
RO_Langs[i] = Lang_Array1[i];
}
}
break;
}
}
break;
}
}
break;
}
}
That way i get what i want but i want to improve it somehow.
The arrays:
HU_Langs =["HU","blah","blah","blah"];
EN_Langs =["EN","blah","blah","blah"];
DE_Langs =["DE","blah","blah","blah"];
etc...
So how can i improve this code without nested for loops?
EDIT: Thank you for all! All the answers are very very good.
My question wasnt clear and detailed enough but i solved it like this with the help of the correct answer.
Here is the function now:
function Get_Language_Object(Lang_Array){
Lang_Array = Lang_Array.replace(/(\r\n|\n|\r)/gm, "");
var langs = ['HU', 'EN', 'DE', 'RO'];
var isLang = str => langs.includes(str);
var { HU: New_HU_Langs, EN: New_EN_Langs, DE: New_DE_Langs, RO: New_RO_Langs } = Lang_Array.split(',')
.reduce((r, str) => {
if(isLang(str)) r.push([]);
r[r.length - 1].push(str);
return r;
}, [])
.reduce((r, [code, ...arr]) => ({ ...r, [code]: arr }), {});
for(var i = 0; i < TAGS.length;i++){
arrLang.HU[TAGS[i]] = New_HU_Langs[i];
arrLang.EN[TAGS[i]] = New_EN_Langs[i];
arrLang.DE[TAGS[i]] = New_DE_Langs[i];
arrLang.RO[TAGS[i]] = New_RO_Langs[i];
}
Set_Actual_Language();
VNotify("Settings Notfy","Lang Set!","success",1500,"success32.png");
}
Split the string by the delimiter (,), reduce the array, and for every language code, add a new sub-array. Push all items to the last sub-array:
var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
var langs = ['HU', 'EN', 'DE', 'RO'];
var isLang = str => langs.includes(str);
var result = Lang_Array1.split(',')
.reduce((r, str) => {
if(isLang(str)) r.push([]);
r[r.length - 1].push(str);
return r;
}, []);
console.log(result);
If you want to split to multiple arrays, reduce the sub-arrays to an object, and use desturcturing to assign them to variables:
var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
var langs = ['HU', 'EN', 'DE', 'RO'];
var isLang = str => langs.includes(str);
var { HU: HU_Langs, EN: EN_Langs, DE: DE_langs, RO: RO_langs } = Lang_Array1.split(',')
.reduce((r, str) => {
if(isLang(str)) r.push([]);
r[r.length - 1].push(str);
return r;
}, [])
.reduce((r, [code, ...arr]) => ({ ...r, [code]: arr }), {});
console.log(HU_Langs, EN_Langs, DE_langs, RO_langs);
You should reduce the list of words after splitting on your delimiter (,). Each time you run into a known key, you alter the key that refers to the current language.
This is the most succinct example:
let arr = "HU,blaf,blaf,blaf,EN,blah,blah,blah,blah,DE,bla,bla,bla,RO,bah,bah,bah"
let keys = [ "DE", "EN", "HU", "RO" ]
let dict = langDict(arr, keys)
console.log(dict)
function langDict(arr, keys) {
let key = null
return arr.split(/,/g).reduce((dict, token) => {
if (Object.keys(dict).length && key == null) {
throw new Error('No language defined yet!')
} else if (keys.includes(token)) {
key = token
} else {
if (dict[key] == null) dict[key] = []
dict[key].push(token)
}
return dict
}, {})
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
As I believe the upper case ISO2 code is the language hint, you could group all results together in a more scalable way.
var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
var Languages = {};
var current = '';
Lang_Array1.split(',').forEach(function (value) {
if (/^[A-Z]{2}$/.test(value))
current = value;
else
Languages[current] = (Languages[current] || []).concat(value);
});
console.log(Languages);
Above snippet would populate the Languages object like this:
{
"HU": [
"blah",
"blah",
"blah"
],
"EN": [
"blah",
"blah",
"blah",
"blah"
],
"DE": [
"blah",
"blah",
"blah"
],
"RO": [
"blah",
"blah",
"blah"
]
}
At this point, all you have to do is to address directly Languages.EN or others, or recreate the original structure via:
var Lang_Array = Object.keys(Languages).reduce(
function (arr, key) {
return arr.concat(key, Languages[key]);
},
[]
);
You could also create arrays with the language starting at index 0, and values following:
var Lang_EN = ['EN'].concat(Languages.EN);
As summary, grouping by keys seem the best way to represent, and manipulate, such flattened structure.
I hope this helped 👋
If the order is constant:
var Lang_Array1 = "HU,blah1,blah2,blah3,EN,blah4,blah5,blah6,blah7,DE,blah8,blah9,blah10,RO,blah11,blah12,blah13";
const arr1 = Lang_Array1.split(/[HU,EN,DE,RO]/g);
let obj = {hu:[],en:[],de:[],ro:[]};
const keys = Object.keys(obj);
let i = 0;
arr1.forEach(el=>{
if(el !== '')
obj[ keys[i] ].push(el);
else
if(obj[ keys[i] ].length > 0)
i++;
});
console.log(obj);
Response:
{hu: Array(3), en: Array(4), de: Array(3), ro: Array(3)}
hu: (3) ["blah1", "blah2", "blah3"]
en: (4) ["blah4", "blah5", "blah6", "blah7"]
de: (3) ["blah8", "blah9", "blah10"]
ro: (3) ["blah11", "blah12", "blah13"]
Temporary array can be used to reference the array to push to :
var Lang_Array = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
var Lang_Array1 = Lang_Array.split(","), HU_Langs = [], EN_Langs = [], DE_Langs = [], RO_Langs = [];
for (var Langs = [], i = 0; i < Lang_Array1.length; i++)
{
var str = Lang_Array1[i];
Langs = { HU: HU_Langs, EN: EN_Langs, DE: DE_Langs, RO: RO_Langs }[str] || Langs;
Langs.push(str);
}
console.log( HU_Langs, EN_Langs, DE_Langs, RO_Langs );

Filter unique array values and sum values

I have the following js array:
for(var j = 0; j < array.length; j++){
arr.push([array[j][0],array[j][1],array[j][2]]);
}
And it translates into this:
Number, type, qty
[[12345, "product", "10"],[12345, "product", "15"],[1234567, "other", "10"]]
What I've been trying to do is to filter the unique product number array[j][0] and sum the qty array[j][2] if there's more than one and I was able to do the unique filter by doing the following:
for(var o = 0; o < arr.length; o++){
if (!n[arr[o][1]]){
n[arr[o][1]] = true
r.push(arr[o]);
}
}
I would like your help to figure this out.. What I'm expecting to achieve is something like this:
[[12345, "product", "25"],[1234567, "other", "10"]]
Since product 12345 was repeated I only need to display it once and sum the qty of the other products with the same product number.
var productIndex = {};
var result = [];
for (var i = 0; i < arr.length; i++) {
var productId = arr[i][0];
if (productIndex[productId] === undefined) {
productIndex[productId] = result.length;
result.push(arr[i]);
} else {
var index = productIndex[productId];
result[index][2] = String(+result[index][2] + +arr[i][2]);
}
}
I am sure there are better ways. But I just changed it to an object, added them and changed it back to an array. Here you go:
https://jsfiddle.net/ct6to1Lv/
var a = [[12345, "product", "10"],[12345, "product", "15"],[1234567, "other", "10"]];
var b = {};
var c = [];
for(var i = 0; i < a.length; i++) {
if(!b.hasOwnProperty(a[i][0])) {
b[a[i][0]] = {};
b[a[i][0]]['qty'] = 0;
}
b[a[i][0]]['id'] = a[i][0];
b[a[i][0]]['name'] = a[i][1];
b[a[i][0]]['qty'] += parseInt(a[i][2]);
}
for(key in b) {
c[c.length] = [b[key]['id'], b[key]['name'], b[key]['qty']];
}
$(function(){
$('#console').append(a.toString()+'<br />');
$('#console').append(JSON.stringify(b)+'<br />');
$('#console').append(c.toString());
});
var arr = [[12345, "product", "10"],[12345, "product", "15"],[1234567, "other", "10"]];
var obj = {};
arr.forEach(function(e){
var t = e[0];
if(obj[t]) {
obj[t][2] += +e[2];
} else {
t = [];
t[0] = e[0];
t[1] = e[1];
t[2] = +e[2];
obj[e[0]] = t;
}
});
var res = [];
Object.keys(obj).forEach(function(k) {
res.push(obj[k]);
});
console.log(res);
Result:
[ [ 12345, 'product', 25 ], [ 1234567, 'other', 10 ] ]
To complete the possibillities, here a solution with a temporary object, which is hidden in this.
var data = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"]],
result = function (data) {
var r = [];
data.forEach(function (a) {
if (!this[a[0]]) {
this[a[0]] = [a[0], a[1], 0];
r.push(this[a[0]]);
}
this[a[0]][2] += +a[2];
}, {});
return r;
}(data);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
See Array.prototype.reduce() and Array.prototype.sort()
var list = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"], [12345, "product", "5"]];
//first we sort the array by id
//#pv = previous value; #cv = current value
list.sort(function(pv, cv) {
var a = +pv[0],
b = +cv[0];
return a - b;
});
//reduce the array for repeated elements
//#pv = previous value; #cv = current value
var reduced = list.reduce(function (pv, cv) {
//slice keeps reference when element is an object/array
var last = pv.slice(-1)[0];
if (last === undefined) return [cv];
//compares the id
if (last[0] == cv[0])
last[2] = +last[2] + (+cv[2]); //sums values
else pv.push(cv); //order elements
return pv;
}, []); //[] initial value for #pv
console.log(reduced);

Convert javascript dot notation object to nested object

I'm trying to build a function that would expand an object like :
{
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
}
Into a nested object :
{ab: {cd: {e:'foo', f:'bar'}, g:'foo2'}}
Like this php function : Set::expand()
Without using eval of course.
I believe this is what you're after:
function deepen(obj) {
const result = {};
// For each object path (property key) in the object
for (const objectPath in obj) {
// Split path into component parts
const parts = objectPath.split('.');
// Create sub-objects along path as needed
let target = result;
while (parts.length > 1) {
const part = parts.shift();
target = target[part] = target[part] || {};
}
// Set value at end of path
target[parts[0]] = obj[objectPath]
}
return result;
}
// For example ...
console.log(deepen({
'ab.cd.e': 'foo',
'ab.cd.f': 'bar',
'ab.g': 'foo2'
}));
If you're using Node.js (e.g. - if not cut and paste out of our module), try this package: https://www.npmjs.org/package/dataobject-parser
Built a module that does the forward/reverse operations:
https://github.com/Gigzolo/dataobject-parser
It's designed as a self managed object right now. Used by instantiating an instance of DataObjectParser.
var structured = DataObjectParser.transpose({
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
});
structured.data() returns your nested object:
{ab: {cd: {e:'foo', f:'bar'}, g:'foo2'}}
So here's a working example in JSFiddle:
http://jsfiddle.net/H8Cqx/
Function name is terrible and the code was quickly made, but it should work. Note that this modifies the original object, I am not sure if you wanted to create a new object that is expanded version of the old one.
(function(){
function parseDotNotation( str, val, obj ){
var currentObj = obj,
keys = str.split("."), i, l = keys.length - 1, key;
for( i = 0; i < l; ++i ) {
key = keys[i];
currentObj[key] = currentObj[key] || {};
currentObj = currentObj[key];
}
currentObj[keys[i]] = val;
delete obj[str];
}
Object.expand = function( obj ) {
for( var key in obj ) {
parseDotNotation( key, obj[key], obj );
}
return obj;
};
})();
var expanded = Object.expand({
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
});
JSON.stringify( expanded );
//"{"ab":{"cd":{"e":"foo","f":"bar"},"g":"foo2"}}"
Derived from Esailija's answer, with fixes to support multiple top-level keys.
(function () {
function parseDotNotation(str, val, obj) {
var currentObj = obj,
keys = str.split("."),
i, l = Math.max(1, keys.length - 1),
key;
for (i = 0; i < l; ++i) {
key = keys[i];
currentObj[key] = currentObj[key] || {};
currentObj = currentObj[key];
}
currentObj[keys[i]] = val;
delete obj[str];
}
Object.expand = function (obj) {
for (var key in obj) {
if (key.indexOf(".") !== -1)
{
parseDotNotation(key, obj[key], obj);
}
}
return obj;
};
})();
var obj = {
"pizza": "that",
"this.other": "that",
"alphabets": [1, 2, 3, 4],
"this.thing.that": "this"
}
Outputs:
{
"pizza": "that",
"alphabets": [
1,
2,
3,
4
],
"this": {
"other": "that",
"thing": {
"that": "this"
}
}
}
Fiddle
You could split the key string as path and reduce it for assigning the value by using a default object for unvisited levels.
function setValue(object, path, value) {
var keys = path.split('.'),
last = keys.pop();
keys.reduce((o, k) => o[k] = o[k] || {}, object)[last] = value;
return object;
}
var source = { 'ab.cd.e': 'foo', 'ab.cd.f': 'bar', 'ab.g': 'foo2' },
target = Object
.entries(source)
.reduce((o, [k, v]) => setValue(o, k, v), {});
console.log(target);
You need to convert each string key into object. Using following function you can get desire result.
function convertIntoJSON(obj) {
var o = {}, j, d;
for (var m in obj) {
d = m.split(".");
var startOfObj = o;
for (j = 0; j < d.length ; j += 1) {
if (j == d.length - 1) {
startOfObj[d[j]] = obj[m];
}
else {
startOfObj[d[j]] = startOfObj[d[j]] || {};
startOfObj = startOfObj[d[j]];
}
}
}
return o;
}
Now call this function
var aa = {
'ab.cd.e': 'foo',
'ab.cd.f': 'bar',
'ab.g': 'foo2'
};
var desiredObj = convertIntoJSON(aa);
Something that works, but is probably not the most efficient way to do so (also relies on ECMA 5 Object.keys() method, but that can be easily replaced.
var input = {
'ab.cd.e': 'foo',
'ab.cd.f': 'bar',
'ab.g': 'foo2'
};
function createObjects(parent, chainArray, value) {
if (chainArray.length == 1) {
parent[chainArray[0]] = value;
return parent;
}
else {
parent[chainArray[0]] = parent[chainArray[0]] || {};
return createObjects(parent[chainArray[0]], chainArray.slice(1, chainArray.length), value);
}
}
var keys = Object.keys(input);
var result = {};
for(var i = 0, l = keys.length; i < l; i++)
{
createObjects(result, keys[i].split('.'), input[keys[i]]);
}
JSFiddle is here.
Here is how I do this in one of my applications:
const obj = {
"start.headline": "1 headline",
"start.subHeadline": "subHeadline",
"start.accordion.headline": "2 headline",
"start.accordion.sections.0.content": "content 0",
"start.accordion.sections.0.iconName": "icon 0",
"start.accordion.sections.1.headline": "headline 1",
"start.accordion.sections.1.content": "content 1",
"start.accordion.sections.1.iconName": "icon 1",
"start.accordion.sections.2.headline": "headline 2",
"start.accordion.sections.2.content": "content 2",
"start.accordion.sections.2.iconName": "icon 2",
"end.field": "add headline",
"end.button": "add button",
"end.msgs.success": "success msg",
"end.msgs.error": "error msg",
};
const res = Object.keys(obj).reduce((res, key) => {
const path = key.split('.');
const lastIndex = path.length - 1;
path.reduce(
(acc, k, i, a) => acc[k] = lastIndex === i ?
obj[key] :
acc[k] || (/\d/.test(a[i+1]) ? [] : {}),
res
);
return res;
}, {});
console.log(res);
ES6 one-liner:
const data = {
'ab.cd.e' : 'foo',
'ab.cd.f' : 'bar',
'ab.g' : 'foo2'
}
const result = Object.entries(data).reduce((a,[p,v])=>
(p.split('.').reduce((b,k,i,r)=>(b[k]??=(i===r.length-1?v:{})),a),a),{})
console.log(result)
This is the answer as provided by #broofa, but converted to TypeScript.
type NestedObject = { [key: string]: any };
function objectify(obj: NestedObject): NestedObject {
const result: NestedObject = {};
for (const key in obj) {
let target: NestedObject = result;
const parts = key.split(".");
for (let j = 0; j < parts.length - 1; j++) {
const part = parts[j];
target = target[part] = target[part] || {};
}
target[parts[parts.length - 1]] = obj[key];
}
return result;
}

How to uppercase Javascript object keys?

Anyone know a good way to turn this?:
var obj = [{key1: value1,key2: value2},{key3: value3,key4: value4}];
into:
var obj = [{Key1: value1,Key2: value2},{Key3: value3,Key4: value4}];
Loop through delete and replace:
var obj = [{key1: 1,key2: 1},{key3: 1,key4: 1}];
for(var i = 0; i<obj.length;i++) {
var a = obj[i];
for (var key in a) {
if (a.hasOwnProperty(key)) {
a[key.charAt(0).toUpperCase() + key.substring(1)] = a[key];
delete a[key];
}
}
obj[i] = a;
}
As of 2019 you can use Object.fromEntries:
let populations = {london: 8.9, beijing: 21.54, mumbai: 18.41}; // March 2020
let entries = Object.entries(populations);
let capsEntries = entries.map((entry) => [entry[0][0].toUpperCase() + entry[0].slice(1), entry[1]]);
let capsPopulations = Object.fromEntries(capsEntries);
console.log(capsPopulations);
Another approach (more clean)
import * as _ from 'lodash';
function capitalizeObjectKeys(obj) {
return _.transform(obj, (result, val, key) => {
result[key.charAt(0).toUpperCase() + key.slice(1)] = val;
});
}
https://stackblitz.com/edit/js-hsqutg?embed=1&file=index.js
In my case this code worked well. Both in uppercase and lowercase with map:
to uppercase :
const newDataUpperCase = data.map( function( item ){
for(var key in item){
var upper = key.toUpperCase();
if( upper !== key ){
item[ upper ] = item;
delete item[key];
}
}
return item;
});
to lowercase :
const newDataUpperCase = data.map( function( item ){
for(var key in item){
var lower= key.toLowerCase();
if( lower!== key ){
item[ lower] = item;
delete item[key];
}
}
return item;
});
const transform = (arrObj) => {
let newObj=[];
for(let obj of arrObj) {
let temp=new Object();
let keys = Object.keys(obj);
let values = Object.values(obj);
let i=0;
keys.forEach(key => {
key=key[0].toUpperCase() + key.slice(1);
temp[`${key}`]=values[i++];
});
newObj.push(temp);
}
return newObj;
};
const arrObj = [{first: 'satya', last:'prakash'}, {college: 'AIT'}];
console.log(transform(arrObj));
If you need to capitalize just first letter you can go with Jeremiah's answer
or if you need to capitalize first letter of each word consider the following
let populations = {"london": 8.9, "beijing": 21.54, "mumbai": 18.41, "new york": 19.4};
let entries = Object.entries(populations);
// Capitalize first letter of each word
let capsEntries = entries.map((entry) => [entry[0].replace(/(^\w{1})|(\s+\w{1})/g, letter => letter.toUpperCase()), entry[1]]);
let capsPopulations = Object.fromEntries(capsEntries);
console.log(capsPopulations)
Regex adopted from this answer
Regex Explanation:
(^\w{1}): match first char of string
|: or
(\s{1}\w{1}): match one char that came after one space
g: match all
match => match.toUpperCase(): replace with can take function, so; replace match with upper case match
This will help you:
const griddata = [
{
"id_pk": 238,
"acT_ID": 238,
"desc": "Record 2",
"parent": 0,
"compdays": 5,
"logical": "1",
"quantity": "1",
"lisT_ID": 75,
"empL_ID": 1388,
"default": 8,
"level": "0",
"sortorder": 2,
"vfpRecNo": 0,
"isDeleted": false,
"clientID": 1,
"empl_num": "1388",
"duedate": "05/04/2022"
}
]
const upperCaseKeys = (data) => {
debugger;
let gridData = [];
if (data != null && data != undefined && data.length > 0) {
let keys = Object.keys(data[0]);
let upperCaseKey = [];
for (let j = 0; j < data.length; j++) {
upperCaseKey = [];
for (let i = 0; i < keys.length; i++) {
set(upperCaseKey, keys[i].toUpperCase(), data[j][keys[i]]);
}
upperCaseKey.push(...upperCaseKey);
gridData.push(Object.assign({}, upperCaseKey));
}
}
console.log("upperCaseKeys",gridData)
}
const set = (obj, prop, value) => {
obj[prop] = value;
}
upperCaseKeys(griddata)
Output:
upperCaseKeys [
{
ID_PK: 238,
ACT_ID: 238,
DESC: 'Record 2',
PARENT: 0,
COMPDAYS: 5,
LOGICAL: '1',
QUANTITY: '1',
LIST_ID: 75,
EMPL_ID: 1388,
DEFAULT: 8,
LEVEL: '0',
SORTORDER: 2,
VFPRECNO: 0,
ISDELETED: false,
CLIENTID: 1,
EMPL_NUM: '1388',
DUEDATE: '05/04/2022'
}
]
use lodash's _.capitalize function
_.capitalize('firstName') => FirstName

Categories