Removing properties of an object starting by a parameter - javascript

I would like some advice to return the properties of an object where others are deleted using lodash.
Example :
deletePropsThatStarsWith({a:1, b:2, aa:3}, 'a') //ouputs {b:2}
So far this is what is working but i don't know where i need to go.
var obj = { a: 1, b: 2, aa: 3 }
function deletePropsThatStartsWith(object, prop) {
return _.omit(object, prop)
}
console.log(deletePropsThatStartsWith(obj, 'a'))
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.10/lodash.min.js"></script>
Thanks for help/advices.

You're looking for the lodash _.omitBy method. You can pass a callback for the second parameter, which decides which properties to omit. The callback will be invoked with two arguments, value and key.
So for your example, all you need is to check the key, and see if it starts with the desired string. The .startsWith method will be a good choice, or if you can't use that, lodash itself provides a _.startsWith method.
Example:
var obj = {a: 1, b: 2, aa: 3}
function deletePropsThatStartsWith(object, prop) {
return _.omitBy(object, (v, k) => k.startsWith(prop))
}
console.log(deletePropsThatStartsWith(obj, 'a'))
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.10/lodash.min.js"></script>

You don't need lodash for this, try to implement it by yourself:
function deletePropsThatStartsWith(object, letter) {
var newObjc = {}
Object.keys(object).forEach((key) => {
if (!key.startsWith(letter)) {
newObjc = { ...newObjc, [key]: object[key] }
}
})
return newObjc
}
const objc = { a: 1, b: 2, aa: 3 }
console.log(deletePropsThatStartsWith(objc, 'a'))
The output is { b: 2 }

Alternatively, you can get the object keys and not use lodash at all. This is built into vanilla js (my favorite JS framework!)
function deletePropsThatStartsWith(oldObject, prop) {
let newObject = {};
Object.keys(oldObject).forEach(k => {
if (k.startsWith(prop)) continue;
newObject[k] = oldObject[k];
});
return newObject;
};

// without lodash
function deletePropsThatStartsWith(obj, prop) {
var keys = Object.keys(obj);
var newObj = {};
for(var i = 0;i < keys.length;i++){
if(keys[i][0] !== prop){
newObj[keys[i]] = obj[keys[i]]
}
}
return newObj;
}
console.log(deletePropsThatStartsWith({a:1,b:2,aa:3},'a'));

You can do this with a simple for..in loop:
function deletePropsThatStartsWith(object, prop) {
let copy = {};
for (key in object) {
if (!key.startsWith(prop))
copy[key] = object[key]
};
return copy;
}
var obj = { a: 1, b: 2, aa: 3 }
console.log(deletePropsThatStartsWith(obj, "a"));

Related

Create a non-mutable recursive 'map' of an object [duplicate]

I am trying to create a deep copy map method for my Redux project that will work with objects rather than arrays. I read that in Redux each state should not change anything in the previous states.
export const mapCopy = (object, callback) => {
return Object.keys(object).reduce(function (output, key) {
output[key] = callback.call(this, {...object[key]});
return output;
},
{});
}
It works:
return mapCopy(state, e => {
if (e.id === action.id) {
e.title = 'new item';
}
return e;
})
However it does not deep copy inner items so I need to tweak it to:
export const mapCopy = (object, callback) => {
return Object.keys(object).reduce(function (output, key) {
let newObject = {...object[key]};
newObject.style = {...newObject.style};
newObject.data = {...newObject.data};
output[key] = callback.call(this, newObject);
return output;
}, {});
}
This is less elegant as it requires to know which objects are passed.
Is there a way in ES6 to use the spread syntax to deep copy an object?
Use JSON for deep copy
var newObject = JSON.parse(JSON.stringify(oldObject))
var oldObject = {
name: 'A',
address: {
street: 'Station Road',
city: 'Pune'
}
}
var newObject = JSON.parse(JSON.stringify(oldObject));
newObject.address.city = 'Delhi';
console.log('newObject');
console.log(newObject);
console.log('oldObject');
console.log(oldObject);
No such functionality is built-in to ES6. I think you have a couple of options depending on what you want to do.
If you really want to deep copy:
Use a library. For example, lodash has a cloneDeep method.
Implement your own cloning function.
Alternative Solution To Your Specific Problem (No Deep Copy)
However, I think, if you're willing to change a couple things, you can save yourself some work. I'm assuming you control all call sites to your function.
Specify that all callbacks passed to mapCopy must return new objects instead of mutating the existing object. For example:
mapCopy(state, e => {
if (e.id === action.id) {
return Object.assign({}, e, {
title: 'new item'
});
} else {
return e;
}
});
This makes use of Object.assign to create a new object, sets properties of e on that new object, then sets a new title on that new object. This means you never mutate existing objects and only create new ones when necessary.
mapCopy can be really simple now:
export const mapCopy = (object, callback) => {
return Object.keys(object).reduce(function (output, key) {
output[key] = callback.call(this, object[key]);
return output;
}, {});
}
Essentially, mapCopy is trusting its callers to do the right thing. This is why I said this assumes you control all call sites.
From MDN
Note: Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays as the following example shows (it's the same with Object.assign() and spread syntax).
Personally, I suggest using Lodash's cloneDeep function for multi-level object/array cloning.
Here is a working example:
const arr1 = [{ 'a': 1 }];
const arr2 = [...arr1];
const arr3 = _.clone(arr1);
const arr4 = arr1.slice();
const arr5 = _.cloneDeep(arr1);
const arr6 = [...{...arr1}]; // a bit ugly syntax but it is working!
// first level
console.log(arr1 === arr2); // false
console.log(arr1 === arr3); // false
console.log(arr1 === arr4); // false
console.log(arr1 === arr5); // false
console.log(arr1 === arr6); // false
// second level
console.log(arr1[0] === arr2[0]); // true
console.log(arr1[0] === arr3[0]); // true
console.log(arr1[0] === arr4[0]); // true
console.log(arr1[0] === arr5[0]); // false
console.log(arr1[0] === arr6[0]); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
I often use this:
function deepCopy(obj) {
if(typeof obj !== 'object' || obj === null) {
return obj;
}
if(obj instanceof Date) {
return new Date(obj.getTime());
}
if(obj instanceof Array) {
return obj.reduce((arr, item, i) => {
arr[i] = deepCopy(item);
return arr;
}, []);
}
if(obj instanceof Object) {
return Object.keys(obj).reduce((newObj, key) => {
newObj[key] = deepCopy(obj[key]);
return newObj;
}, {})
}
}
You can use structuredClone() like the following:
const myOriginal = {
title: "Full Stack JavaScript Developer",
info: {
firstname: "Abolfazl",
surname: "Roshanzamir",
age: 34
}
};
const myDeepCopy = structuredClone(myOriginal);
structuredClone()
You can use structuredClone() that is a built-in function for deep-copying.
Structured cloning addresses many (although not all) shortcomings of the JSON.stringify() technique.
Structured cloning can handle cyclical data structures,
support many built-in data types, and is generally more robust and often faster.
However, it still has some limitations that may catch you off-guard:
1-Prototypes : If you use structuredClone() with a class instance,
you’ll get a plain object as the return value, as structured cloning discards the object’s prototype chain.
2-Functions: If your object contains functions, they will be quietly discarded.
3- Non-cloneables: Some values are not structured cloneable, most notably Error and DOM nodes. It will cause structuredClone() to throw.
const myDeepCopy = structuredClone(myOriginal);
JSON.stringify
If you simply want to deep copy the object to another object,
all you will need to do is JSON.stringify the object and parse it using JSON.parse afterward.
This will essentially perform deep copying of the object.
let user1 = {
name: 'Abolfazl Roshanzamir',
age: 34,
university: {
name: 'Shiraz Bahonar University'
}
};
let user2 = JSON.parse(JSON.stringify(user1));
user2.name = 'Andy Madadian';
user2.university.name = 'Kerman Bahonar University'
console.log(user2);
// { name: 'Andy Madadian', age: 33, university: { name: 'Kerman Bahonar University' } }
console.log(user1);
// { name: 'Abolfazl Roshanzamir', age: 33, university: { name: 'Shiraz Bahonar University' } }
Spread operator / Object.assign()
One way to create a shallow copy in JavaScript using the object spread operator ... or Object.assign() like the following:
const myShallowCopySpread = {...myOriginal};
const myShallowCopyObjectAssign=Object.assign({},obj)
Performance
When it comes to performance the creator Surma has pointed out that JSON.Parse() can be a bit faster for small objects. But when you have a large object, complex object
structuredClone() starts to get significantly faster.
Browser support is pretty fantastic And even is supported by Node.js.
const a = {
foods: {
dinner: 'Pasta'
}
}
let b = JSON.parse(JSON.stringify(a))
b.foods.dinner = 'Soup'
console.log(b.foods.dinner) // Soup
console.log(a.foods.dinner) // Pasta
Using JSON.stringify and JSON.parse is the best way. Because by using the spread operator we will not get the efficient answer when the json object contains another object inside it. we need to manually specify that.
Here's my deep copy algorithm.
const DeepClone = (obj) => {
if(obj===null||typeof(obj)!=='object')return null;
let newObj = { ...obj };
for (let prop in obj) {
if (
typeof obj[prop] === "object" ||
typeof obj[prop] === "function"
) {
newObj[prop] = DeepClone(obj[prop]);
}
}
return newObj;
};
// use: clone( <thing to copy> ) returns <new copy>
// untested use at own risk
function clone(o, m){
// return non object values
if('object' !==typeof o) return o
// m: a map of old refs to new object refs to stop recursion
if('object' !==typeof m || null ===m) m =new WeakMap()
var n =m.get(o)
if('undefined' !==typeof n) return n
// shallow/leaf clone object
var c =Object.getPrototypeOf(o).constructor
// TODO: specialize copies for expected built in types i.e. Date etc
switch(c) {
// shouldn't be copied, keep reference
case Boolean:
case Error:
case Function:
case Number:
case Promise:
case String:
case Symbol:
case WeakMap:
case WeakSet:
n =o
break;
// array like/collection objects
case Array:
m.set(o, n =o.slice(0))
// recursive copy for child objects
n.forEach(function(v,i){
if('object' ===typeof v) n[i] =clone(v, m)
});
break;
case ArrayBuffer:
m.set(o, n =o.slice(0))
break;
case DataView:
m.set(o, n =new (c)(clone(o.buffer, m), o.byteOffset, o.byteLength))
break;
case Map:
case Set:
m.set(o, n =new (c)(clone(Array.from(o.entries()), m)))
break;
case Int8Array:
case Uint8Array:
case Uint8ClampedArray:
case Int16Array:
case Uint16Array:
case Int32Array:
case Uint32Array:
case Float32Array:
case Float64Array:
m.set(o, n =new (c)(clone(o.buffer, m), o.byteOffset, o.length))
break;
// use built in copy constructor
case Date:
case RegExp:
m.set(o, n =new (c)(o))
break;
// fallback generic object copy
default:
m.set(o, n =Object.assign(new (c)(), o))
// recursive copy for child objects
for(c in n) if('object' ===typeof n[c]) n[c] =clone(n[c], m)
}
return n
}
Here is the deepClone function which handles all primitive, array, object, function data types
function deepClone(obj){
if(Array.isArray(obj)){
var arr = [];
for (var i = 0; i < obj.length; i++) {
arr[i] = deepClone(obj[i]);
}
return arr;
}
if(typeof(obj) == "object"){
var cloned = {};
for(let key in obj){
cloned[key] = deepClone(obj[key])
}
return cloned;
}
return obj;
}
console.log( deepClone(1) )
console.log( deepClone('abc') )
console.log( deepClone([1,2]) )
console.log( deepClone({a: 'abc', b: 'def'}) )
console.log( deepClone({
a: 'a',
num: 123,
func: function(){'hello'},
arr: [[1,2,3,[4,5]], 'def'],
obj: {
one: {
two: {
three: 3
}
}
}
}) )
function deepclone(obj) {
let newObj = {};
if (typeof obj === 'object') {
for (let key in obj) {
let property = obj[key],
type = typeof property;
switch (type) {
case 'object':
if( Object.prototype.toString.call( property ) === '[object Array]' ) {
newObj[key] = [];
for (let item of property) {
newObj[key].push(this.deepclone(item))
}
} else {
newObj[key] = deepclone(property);
}
break;
default:
newObj[key] = property;
break;
}
}
return newObj
} else {
return obj;
}
}
const cloneData = (dataArray) => {
newData= []
dataArray.forEach((value) => {
newData.push({...value})
})
return newData
}
a = [{name:"siva"}, {name:"siva1"}] ;
b = myCopy(a)
b === a // false`
I myself landed on these answers last day, trying to find a way to deep copy complex structures, which may include recursive links. As I wasn't satisfied with anything being suggested before, I implemented this wheel myself. And it works quite well. Hope it helps someone.
Example usage:
OriginalStruct.deep_copy = deep_copy; // attach the function as a method
TheClone = OriginalStruct.deep_copy();
Please look at https://github.com/latitov/JS_DeepCopy for live examples how to use it, and also deep_print() is there.
If you need it quick, right here's the source of deep_copy() function:
function deep_copy() {
'use strict'; // required for undef test of 'this' below
// Copyright (c) 2019, Leonid Titov, Mentions Highly Appreciated.
var id_cnt = 1;
var all_old_objects = {};
var all_new_objects = {};
var root_obj = this;
if (root_obj === undefined) {
console.log(`deep_copy() error: wrong call context`);
return;
}
var new_obj = copy_obj(root_obj);
for (var id in all_old_objects) {
delete all_old_objects[id].__temp_id;
}
return new_obj;
//
function copy_obj(o) {
var new_obj = {};
if (o.__temp_id === undefined) {
o.__temp_id = id_cnt;
all_old_objects[id_cnt] = o;
all_new_objects[id_cnt] = new_obj;
id_cnt ++;
for (var prop in o) {
if (o[prop] instanceof Array) {
new_obj[prop] = copy_array(o[prop]);
}
else if (o[prop] instanceof Object) {
new_obj[prop] = copy_obj(o[prop]);
}
else if (prop === '__temp_id') {
continue;
}
else {
new_obj[prop] = o[prop];
}
}
}
else {
new_obj = all_new_objects[o.__temp_id];
}
return new_obj;
}
function copy_array(a) {
var new_array = [];
if (a.__temp_id === undefined) {
a.__temp_id = id_cnt;
all_old_objects[id_cnt] = a;
all_new_objects[id_cnt] = new_array;
id_cnt ++;
a.forEach((v,i) => {
if (v instanceof Array) {
new_array[i] = copy_array(v);
}
else if (v instanceof Object) {
new_array[i] = copy_object(v);
}
else {
new_array[i] = v;
}
});
}
else {
new_array = all_new_objects[a.__temp_id];
}
return new_array;
}
}
Cheers#!
I would suggest using the spread operator. You'll need to spread a second time if you need to update the second level. Attempting to update the newObject using something like newObject.address.city will throw an error if address did not already exist in oldObject.
const oldObject = {
name: 'A',
address: {
street: 'Station Road',
city: 'Pune'
}
}
const newObject = {
...oldObject,
address: {
...oldObject.address,
city: 'Delhi'
}
}
console.log(newObject)
This is a very old question but I think in 2022 there are many ways to solve this. However, if you want a simple, fast and vanilla JS solution check this out:
const cloner = (o) => {
let idx = 1
const isArray = (a) => a instanceof Array
const isObject = (o) => o instanceof Object
const isUndefined = (a) => a === undefined
const process = v => {
if (isArray(v)) return cloneArray(v)
else if (isObject(v)) return cloneObject(v)
else return v
}
const register = (old, o) => {
old.__idx = idx
oldObjects[idx] = old
newObjects[idx] = o
idx++
}
const cloneObject = o => {
if (!isUndefined(o.__idx)) return newObjects[o.__idx]
const obj = {}
for (const prop in o) {
if (prop === '__idx') continue
obj[prop] = process(o[prop])
}
register(o, obj)
return obj
}
const cloneArray = a => {
if (!isUndefined(a.__idx)) return newObjects[a.__idx]
const arr = a.map((v) => process(v))
register(a, arr)
return arr
}
const oldObjects = {}
const newObjects = {}
let tmp
if (isArray(o)) tmp = cloneArray(o)
else if (isObject(o)) tmp = cloneObject(o)
else return o
for (const id in oldObjects) delete oldObjects[id].__idx
return tmp
}
const c = {
id: 123,
label: "Lala",
values: ['char', 1, {flag: true}, [1,2,3,4,5], ['a', 'b']],
name: undefined
}
const d = cloner(c)
d.name = "Super"
d.values[2].flag = false
d.values[3] = [6,7,8]
console.log({ c, d })
It's recursive and self-contained, all the functions needed are defined in the function cloner().
In this snippet we are handling Array and Object types if you want to add more handlers you can add specify handlers like Date and clone it like new Date(v.getTime())
For me Array and Object are the types that I use the most in my implementations.

Function copies objects in JavaScript by calling itself, Need help understanding the logic

I found this JavaScript code for copying Objects, the code is doing what it's suppose to do, but what I don't understand is when the function call itself; how come newObject in the first iteration desn't loose its value, it should be overwritten when the function called itself and created a new newObject ? does that mean that when a function call itself it still keeps a copy of the first newObject created before it called itself?
const o = {
a: 'a',
b: 'b',
obj: {
key: 'key',
},
}
const o2 = o
o2.a = 'new value'
// o and o2 reference the same object
console.log(o.a)
// this shallow-copies o into o3
const o3 = Object.assign({}, o)
// deep copy
function deepCopy(obj) {
// check if vals are objects
// if so, copy that object (deep copy)
// else return the value
const keys = Object.keys(obj)
const newObject = {}
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (typeof obj[key] === 'object') {
newObject[key] = deepCopy(obj[key])
} else {
newObject[key] = obj[key]
}
}
return newObject
}
const o4 = deepCopy(o)
o.obj.key = 'new key!'
console.log(o4.obj.key)
Recursive functions can be confusing. A few well placed console.log()s or running the code in a debugger can really help. The function makes a newObject for the original object and each child object in the object. As the recursion unwinds it sets the property in the parent to the result of the recursive call on the child.
You can see the effect in the console.logs here:
const o = {
a: 'a',
b: 'b',
obj: {
key: 'key',
deeper: {one: 1, two: 2}
},
}
// deep copy
function deepCopy(obj) {
console.log("deep copy of: ", obj)
const keys = Object.keys(obj)
const newObject = {}
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (typeof obj[key] === 'object') {
console.log("setting child of", key, "to:")
newObject[key] = deepCopy(obj[key])
} else {
newObject[key] = obj[key]
}
}
return newObject
}
console.log("starting with")
const o4 = deepCopy(o)
Each of the lines starting with deep copy of indicates a newly created newObject in a recursive call, but the only newObject returned is the first one — all the others get set as children.

Change references of objects to another object in deeply nested object

How do I set object references of one type with another object reference in a deeply nested object?
// Following is the class
class Ab{ a: number; }
let x = new Ab();
x.a = 10;
// Now I have an object something like this
let obj = {
b: 'hello',
c: new Ab(),
d: {
x: 5,
y: new Ab(),
z: {
p: new Ab()
}
}
}
Now I should be able to change all the references of new Ab() set to x object.
let obj = {
b: 'hello',
c: x, // reassign the x reference
d: {
x: 5,
y: x,
z: {
p: x
}
}
}
I think I have to go with deep cloning the object. I was using lodash
lodash.cloneDeepWith(obj, val => {
// please do not worry/consider these logic this condition is working
if (Object.getPrototypeOf(val) === tc) {
// Actual problem is here
// reassigning is not changing the original reference of obj
val = x;
}
});
Are there any solutions out there which are faster and easier? Any help is appreciated.
The following code will recursively change each object based on the instance type:
const changeObjects = (obj, replacement, className) => {
Object.keys(obj).forEach((key) => {
if (obj[key] instanceof className) {
obj[key] = replacement;
} else if (typeof obj[key] === 'object') {
changeObjects(obj[key], replacement, className);
}
});
}
changeObjects(obj, x, Ab);
That code should work with the example you gave without creating a deep clone. If you did want to clone the replacement and not have circular references you could do this as well:
obj[key] = Object.assign({}, replacement);
when assigning the replacement.
I'm not sure if this qualifies as easier, but it is a bit faster and does the job you need it to do.

lodash _.get function in typescript

I get the feeling after some googling that a lot of lodash's functions can be achieved with native typescript but i cannot find a straightforward answer for the _.get function...
In lodash the following, using the _.get function alerts 1
let obj = {a:{b:1}};
let a = _.get(obj, 'a.b');
alert(a);
Is there a way of achieving the same result with only typescript?
In plain Javascript you could split the path and reduce the path by walking the given object.
function getValue(object, path) {
return path.
replace(/\[/g, '.').
replace(/\]/g, '').
split('.').
reduce((o, k) => (o || {})[k], object);
}
var obj = { a: { b: 1 } },
a = getValue(obj, 'a.b');
console.log(a);
/**
* Get value of a property from a nested object.
* Example:
* var x = { a: {b: "c"} };
* var valueOf_b = getDeepValue(x, ["a", "b"]);
*
* #param {object} Object The Object to get value from
* #param {KeyArray} Array[String] An array of nested properties. ex. ["property", "childProperty"]
*/
const getDeepValue = (object, keyArray) => {
const extractValue = (obj, kArray) => {
const objProperty = obj[kArray[0]];
if (kArray.length >= 1) {
const newKeyArray = kArray.splice(1, kArray.length);
if (newKeyArray.length === 0) return objProperty;
return extractValue(objProperty, newKeyArray);
}
return objProperty;
};
try {
const value = extractValue(object, keyArray.slice());
if (value === undefined || typeof value === 'object') {
console.warn("Unable to retrieve value from object for key ", keyArray);
return '';
} else {
return value;
}
} catch (e) {
console.warn("Exception: Unable to retrieve value from object for key ", keyArray);
return '';
}
};
Maybe slightly cleaner alternative using an ES6 default parameter:
const get = (o, path) => path.split('.').reduce((o = {}, key) => o[key], o);
console.log(get({ a: { b: 43 } }, 'a.b')); // 43
The above digs all the way to the bottom even when it encounters undefined.
An alternative is recursion, you'll have to split before invoking it:
function get(object, [head, ...tail]) {
object = object[head];
return tail.length && object ? get(object, tail) : object;
}
console.log(get({ a: { b: 43 } }, 'a.b'.split('.'))); // 43

Flatten object to array?

I'm using an object as a hash table. I'd like to quickly print out its contents (for alert() for instance). Is there anything built in to convert a hash into arrays of (key, value) pairs?
Since you want to alert it out I assume it's not for your production version, and that old browser compatibility is not an issue.
If this is the case, then you can do this:
var myHash = ......
alert(Object.keys(myHash).map(function(key) { return [key, myHash[key]]; }));
I updated this some more. This is much easier to parse than even console.log because it leaves out the extra stuff that's in there like __proto__.
function flatten(obj) {
var empty = true;
if (obj instanceof Array) {
str = '[';
empty = true;
for (var i=0;i<obj.length;i++) {
empty = false;
str += flatten(obj[i])+', ';
}
return (empty?str:str.slice(0,-2))+']';
} else if (obj instanceof Object) {
str = '{';
empty = true;
for (i in obj) {
empty = false;
str += i+'->'+flatten(obj[i])+', ';
}
return (empty?str:str.slice(0,-2))+'}';
} else {
return obj; // not an obj, don't stringify me
}
}
The only thing I would do to improve this is have it indent correctly based on recursion level.
for quick & dirty use in alert you could use JSON:
alert(JSON.stringify(yourObj).replace(/,/g,'\n'));
Not that I'm aware of. Still, you can do it yourself fairly concisely:
var obj = { a: 1, b: 2, c: 3 };
var arr = [];
for (var i in obj) {
var e = {};
e[i] = obj[i];
arr.push(e);
}
console.log(arr);
// Output: [Object { a=1 }, Object { b=2 }, Object { c=3 }]
Of course, you can't alert this either, so you might as well just console.log(obj) in the first place.
You could output arrays of arrays:
var obj = { a: 1, b: 2, c: 3 };
var arr = [];
for (var i in obj) {
arr.push([i, obj[i]]);
}
console.log(arr);
// Output: [["a", 1], ["b", 2], ["c", 3]]
alert(arr);
// Alert: a, 1, b, 2, c, 3
But, again, ew.
Here is my version of it. It should allow you to flatten input like below:
var input = {
a: 'asdf',
b: [1,2,3],
c: [[1,2],[3,4]],
d: {subA: [1,2]}
}
The function is like this:
function flatten (input, output) {
if (isArray(input)) {
for(var index = 0, length = input.length; index < length; index++){
flatten(input[index], output);
}
}
else if (isObject(input)) {
for(var item in input){
if(input.hasOwnProperty(item)){
flatten(input[item], output);
}
}
}
else {
return output.push(input);
}
};
function isArray(obj) {
return Array.isArray(obj) || obj.toString() === '[object Array]';
}
function isObject(obj) {
return obj === Object(obj);
}
Usage is something like:
var output = []
flatten(input, output);
Then output should be the flattened array.
Maybe a little late, but here you have my version of the answer, updated to ES2015. I use a recursive function and it works even if there are other objects inside the main object:
function objectFlattener (object) {
return Reflect.apply(Array.prototype.concat, [], Object.keys(object).map(key => {
if (object[key] instanceof Object) {
return objectFlattener(object[key]);
}
return `${ key }: ${ object[key] }`;
}));
}
So changing the last return you can format the element inside your array.
Use the for loop:
for (var x in yourObj)
{
alert(yourObj[x]);
}

Categories

Resources