Reorder json in js - javascript

I am building a tree, and the order I want is name, desc, then children.
So the json I got is in different order. How do you reorder it or is there a better way to write this code, taking consideration of multiple branches, and may be extra element can be add to the input?
So here is my code:
var arry = [{
"name": "J",
"target": "L",
"desc": "2"
},
{
"name": "L",
"target": "A",
"desc": "1"
},
{
"name": "S",
"target": "L",
"desc": "3"
}
];
function toJSON(data) {
var root = data.find(function(x) {
return !data.some(function(y) {
return y.name === x.target
});
}).target;
console.log(root)
var desc = data.find(function(x) {
return !data.some(function(y) {
return y.name === x.target
});
}).desc;
var b = data.reduce(function(acc, x) {
acc[x.target] = acc[x.target] || [];
acc[x.target].push(x.name);
return acc;
}, {});
var tree = buildTree(root, b);
function buildTree(name, branches, desc) {
var tree = {
'name': name
};
if (branches[name]) {
tree.children = branches[name].map(function(x) {
return buildTree(x, branches, desc)
});
for (var child in branches[name]) {
var x = arry.find(function(i) {
return (i.name === branches[name][child] && i.target === name)
})
tree.children[child].desc = x.desc
}
}
return tree;
}
if (tree.name === root) {
tree.desc = root
}
for (var i in tree) {
console.log(i)
}
return tree;
} // JavaScript name code
var a = toJSON(arry)
console.log(JSON.stringify(a, null, 2))
The result I got is:
{
"name": "A",
"children": [
{
"name": "L",
"children": [
{
"name": "J",
"desc": "2"
},
{
"name": "S",
"desc": "3"
}
],
"desc": "1"
}
],
"desc": "A"
}
The result I want is:
{
"name": "A",
"desc": "A",
"children": [
{
"name": "L",
"desc": "1",
"children": [
{
"name": "J",
"desc": "2"
},
{
"name": "S",
"desc": "3"
}
]
}
]
}

I agree with the question "Why does order matter"
But if you really want...
When creating object literals the keys/properties remain in the order in which they are/were defined (unless the keys are integers, then they will be in numerical order). On the line where you create your tree node you define an object but only put the name property on it. You then add children, then finally set the desc property. If you instead define your object right away they will have that order and you can later mutate the values however you want.
const tree = {
name,
desc: undefined,
children: undefined,
};
var arry = [
{
name: "J",
target: "L",
desc: "2"
},
{
name: "L",
target: "A",
desc: "1"
},
{
name: "S",
target: "L",
desc: "3"
}
];
function toJSON(data) {
var root = data.find(function(x) {
return !data.some(function(y) {
return y.name === x.target;
});
}).target;
console.log(root);
var desc = data.find(function(x) {
return !data.some(function(y) {
return y.name === x.target;
});
}).desc;
var b = data.reduce(function(acc, x) {
acc[x.target] = acc[x.target] || [];
acc[x.target].push(x.name);
return acc;
}, {});
var tree = buildTree(root, b);
function buildTree(name, branches, desc) {
// define your tree node shape here
var tree = {
name,
desc: undefined,
children: undefined
};
if (branches[name]) {
tree.children = branches[name].map(function(x) {
return buildTree(x, branches, desc);
});
for (var child in branches[name]) {
var x = arry.find(function(i) {
return i.name === branches[name][child] && i.target === name;
});
tree.children[child].desc = x.desc;
}
}
return tree;
}
if (tree.name === root) {
tree.desc = root;
}
for (var i in tree) {
console.log(i);
}
return tree;
} // JavaScript name code
var a = toJSON(arry);
console.log(JSON.stringify(a, null, 2));

Related

how to filter object from nested objects by specific unique key

This is input data, where i want filter a object by ID key
let myData = {
"nodeInfo":
{
"9":
{
"1": { "ID": "14835", "name": "Binod" },
"2": { "ID": "14836", "name": "Rahul" },
"3": { "ID": "14837", "name": "Sunil" },
},
"10":
{
"4": { "ID": "14839", "name": "Shikhar" },
"5": { "ID": "14840", "name": "Hari" },
"6": { "ID": "14841", "name": "Harsh" },
}
}
};
i want which object who have ID value 14835
so my result would be:: { "ID": "14835", "name": "Binod" }
This supports many levels of nested objects
function isObject(possibleObject) {
return typeof possibleObject === 'object' && possibleObject !== null
}
function find(data, key) {
for (const element of Object.values(data)) {
if (element.ID) {
if (element.ID === key) {
return element;
}
continue;
}
if (isObject(element)) {
const foundElement = find(element, key);
if (foundElement !== null) {
return foundElement;
}
}
}
// Not found
return null;
}
``
#Gluey1017 has provided a very good answer that will return the first occurrence of an object with the given ID key. In the following snippet I modified/extended his script to also collect multiple results should they exist:
const myData = {
"nodeInfo":
{
"9":
{
"1": { "ID": "14835", "name": "Binod" },
"2": { "ID": "14836", "name": "Rahul" },
"3": { "ID": "14837", "name": "Sunil" },
},
"10":
{
"4": { "ID": "14839", "name": "Shikhar" },
"5": { "ID": "14840", "name": "Hari" },
"6": { "ID": "14841", "name": "Harsh" },
},
"15":
{ "7": {
"8": { "ID": "14835" , "name": "Carsten" },
"9": { "ID": "14842" , "name": "someone" }
} }
}
};
function find(data,key){
const found=[]; // results array
function fnd(da) { // recursive function
for (const el of Object.values(da)){
if (el?.ID === key) found.push(el);
if (typeof el==='object' && el !== null) fnd(el);
}
}
fnd(data);
return found;
}
console.log(find(myData,"14835"));
Using an arrow function expression, what about
getter = ID => Object.values(
myData.nodeInfo
).map(
o => Object.values(o).filter(
v => v.ID === ID
)[0]
)
and then
> getter("14835")
< (2) [{…}, {…}]
And, depending on whether your identifiers are unique or not, you can actually do
> getter("14835")[0]
< {ID: '14835', name: 'Binod'}
Using spread operator to find item from a flat array of objects:
// get nodeInfo values
Object.values(myData.nodeInfo)
// reduce to flat array of objects
.reduce((acc,v) => [...acc, ...Object.values(v)],[])
// assuming ID is unique find object
.find(o => o.ID === "14835")
If the level is unknown, with data structured as above, recursion can be used to get to the level where objects have the desired properties.
Please notice that the function expects an array as its first parameter.
// check for the presence of the relevant keys
const findByID = (vals, id) => vals.some(o => 'ID' in o && 'name' in o)
// if present find the value
? vals.find(o => o.ID === id)
// else repeat with level below
: findByID(vals.reduce((acc,v) => [...acc, ...Object.values(v)],[]), id);
findByID(Object.values(myData), "14835");

Get values for a matching key recursively from object

I have this json object
{
"data": {
"user": {
"user_info": {
"id": "AoGC2HQ9vedHmzcMX"
},
"product": [
{
"node": {
"id": "NzcxNzU2ODU1ODM1",
"feedback": {
"raters": {
"nodes": [
{
"id": "1",
"name": "Dan"
},
{
"id": "2",
"name": "Allen"
},
{
"id": "3",
"name": "Williams"
}
]
},
"commentors": {
"nodes": [
{
"id": "001",
"name": "Kent"
},
{
"id": "002",
"name": "Jay"
}
]
}
}
}
}
]
}
}
}
So how do I make it to get values of id If the parent property matches the desired key name, In this example I want to get all id's from raters.nodes only.
so expected result is
[1,2,3]
I know can do obj.data.user.product[0].node.feedback.raters.nodes and loop through that, but that is not how I want and the object tree occasionally changes.
I have used this recursive function
const recursiveSearch = (obj, searchKey, results = []) => {
const r = results;
Object.keys(obj).forEach(key => {
const value = obj[key];
if(key === searchKey && typeof value !== 'object'){
r.push(value);
}else if(typeof value === 'object'){
recursiveSearch(value, searchKey, r);
}
});
return r;
};
//returns all id's
While it works, it returns all id values, so how do I improve it? If not, how do I make this possible?
I think you want to really do this in 2 steps,..
First make a function to get the root node your looking for, and then you can just use map like normal.
Below is an example.
var data = JSON.parse("{\"data\":{\"user\":{\"user_info\":{\"id\":\"AoGC2HQ9vedHmzcMX\"},\"product\":[{\"node\":{\"id\":\"NzcxNzU2ODU1ODM1\",\"feedback\":{\"raters\":{\"nodes\":[{\"id\":\"1\",\"name\":\"Dan\"},{\"id\":\"2\",\"name\":\"Allen\"},{\"id\":\"3\",\"name\":\"Williams\"}]},\"commentors\":{\"nodes\":[{\"id\":\"001\",\"name\":\"Kent\"},{\"id\":\"002\",\"name\":\"Jay\"}]}}}}]}}}");
function getRoot(data, search) {
function get(path, data) {
for (const [k, v] of Object.entries(data)) {
if (v instanceof Object) {
const pp = `${path}.${k}`;
if (pp.slice(-search.length) === search) {
return v;
}
const r = get(`${path}.${k}`, v);
if (r) return r;
}
}
}
return get('', data);
}
const r = getRoot(data, 'raters.nodes');
console.log(r && r.map(i => i.id));

How to update a specific node of a tree structured global json object in javascript?

I passed a global tree structured JSON string in jquery fancy tree. I can add a new node data to the global object. I am not able to update a specific node of the global JSON string although I can find that specific node.
I have tried the following way of doing what I wanted but at the end the global variable is not changed. I also tried recursive way but not worked too. Also this is my first question at stackoverflow. I simplified the data because real data is so big.
// global json object array
var treeViewData = [
{
"nodeData": {
"physicalid": "A",
"rootToNode": "A-0",
"level": "0",
"attribute": "English-english"
},
"children": [
{
"nodeData": {
"physicalid": "B",
"rootToNode": "A-0:B-1",
"level": "1",
"attribute": "Finish-finish"
},
"children": []
},
{
"nodeData": {
"physicalid": "C",
"rootToNode": "A-0:C-1",
"level": "1",
"attribute": "Arabic-arabic"
},
"children": [
{
"nodeData": {
"physicalid": "D",
"rootToNode": "A-0:C-1:D-2",
"level": "2",
"attribute": "Spanish-spanished"
},
"children": []
}
]
}
]
}
]
Here is the update method. rootToNode is unique.
findAndUpdateNodeIterative: function(selfNodeRootPath, updatedNode) {
console.log("+++++++++ findAndUpdateNodeIterative ++++++++");
var thisContext = this;
var currentNode = thisContext.treeViewData[0];
var i, currentChild, result;
var flag = true;
while (flag) {
if (currentNode.nodeData.rootToNode === selfNodeRootPath) {
currentNode.nodeData.attribute = updatedNode.nodeData.attribute;
flag = false;
} else {
let it = true;
for (i = 0; i < currentNode.children.length && it; i += 1) {
currentChild = currentNode.children[i];
var currentNodeLevel = parseInt(currentChild.nodeData.level);
var targetPhysicalId = selfNodeRootPath.split(':')[currentNodeLevel].split("-")[0];
if (targetPhysicalId === currentChild.nodeData.physicalid) {
// Search in the current child
currentNode = currentChild;
it = false;
}
}
}
}
console.log("---------- findAndUpdateNodeIterative ---------");
},
usage
var updatedNode = {
"nodeData": {
"physicalid": "D",
"rootToNode": "A-0:C-1:D-2",
"level": "2",
"attribute": "Spanish-spanish"
},
"children": []
};
thisContext.findAndUpdateNodeIterative(updatedNode.nodeData.rootToNode, updatedNode);
console.log(thisContext.treeViewData); // **not updated**
RequireJS Code structure,
// about 4000 lines are present in this module
define("CustomModule", ["fewModules"], function(fewModules) {
var widget = {
init: function() {
this.treeViewData = '';
},
// many methods are here
update: function() {
var thisContext = this;
// rest api call brings the data nodesMap
for (let selectedObject of nodesMap.values()) {
// many lines
thisContext.findAndUpdateNodeIterative(updatedNode.nodeData.rootToNode, updatedNode);
console.log(thisContext.treeViewData); // not updated treeViewData
}
}
};
return widget;
}

Pick one field from Array of Objects recursively

I am looking for a way using JavaScript / Lodash to retrieve the same hirearchy as the input array of objects but want to retain only selected fields.
I could also formulate this question as doing a deep copy of an array of objects retaining only certain fields.
For example, given the following array:
[
{
"id": "q1",
"text": "Q1 text",
"children": [
{
"id": "q11",
"text": "t",
"children": [
{
"id": "q111",
"text": "t"
},
{
"id": "q112",
"text": "t"
}
]
}
]
},
{
"id": "q2",
"text": "e",
"children": [
{
"id": "q22",
"text": "e"
}
]
},
{
"id": "q3",
"text": "e"
}
]
The output should be as below. This is exactly the same as array of objects above but keeps only id and children's ids. The children can be any level deep.
[
{
"id": "q1",
"children": [
{
"id": "q11",
"children": [
{
"id": "q111",
},
{
"id": "q112"
}
]
}
]
},
{
"id": "q2",
"children": [
{
"id": "q22",
}
]
},
{
"id": "q3"
}
]
You can make a function that takes an array and maps it to objects with just the id and children. To set the id, just copy the id, to set the children on the returned object pass the children array back into the function recursively:
let arr = [{"id": "q1","text": "Q1 text","children": [{"id": "q11","text": "t","children": [{"id": "q111","text": "t"},{"id": "q112","text": "t"}]}]},{"id": "q2","text": "e","children": [{"id": "q22","text": "e"}]},{"id": "q3","text": "e"}]
const justIDs = (arr) => arr.map(({id, children}) => {
let ret = {id}
if(children) ret.children = justIDs(children)
return ret
})
let filtered = justIDs(arr)
console.log(filtered)
and lodash, love lodash, learn lodash...
function omitKeysDeep(input, keys) {
if(!_.isArray(keys)) throw new Error('omitKeys expected an array');
return _.map(input, (elem) => {
if(elem.children) elem.children = omitKeysDeep(elem.children, keys);
return _.omit(elem, keys);
});
}
omitKeysDeep(a, ['text']);
OR... instead of _.omit(..) to remove unwanted keys you could use _.pick(...) to specify only wanted keys:
function pickKeysDeep(input, keys) {
if(!_.isArray(keys)) throw new Error('pickKeys expected an array');
return _.map(input, (elem) => {
if(elem.children) elem.children = pickKeysDeep(elem.children, keys);
return _.pick(elem, keys);
});
}
pickKeysDeep(a, ['id', 'children']);
Here's a non-recursive approach that uses an explicit stack and a set for fast lookup in cases when you have many keys to prune out. This is a general solution that should work on any keys you throw at it and doesn't mutate the original array.
const data = [
{
"id": "q1",
"text": "Q1 text",
"children": [
{
"id": "q11",
"text": "t",
"children": [
{
"id": "q111",
"text": "t"
},
{
"id": "q112",
"text": "t"
}
]
}
]
},
{
"id": "q2",
"text": "e",
"children": [
{
"id": "q22",
"text": "e"
}
]
},
{
"id": "q3",
"text": "e"
}
];
const removeKeys = (arr, keys) => {
const keep = new Set(keys);
const res = [];
const stack = [[arr, res]];
while (stack.length) {
const [curr, cpy] = stack.pop();
if (Array.isArray(curr)) {
curr.forEach((e, i) => {
cpy[i] = {};
for (const k in e) {
if (keep.has(k)) {
cpy[i][k] = e[k];
stack.push([e[k], cpy[i][k]]);
}
}
});
}
}
return res;
};
console.log(JSON.stringify(removeKeys(data, ["id", "children"]), null, 4));
Here's my version which does work recursively.
/**
* Like _.pick() but will also map over arrays implicitly.
* ie. path 'a.b.c' will transform {a:[{b:{c:1,d:2}}]} => {a:[{b:{c:1}}]}
*
* #param {object} o - Object to copy.
* #param {string[]} paths - List of paths to include.
* #returns {mixed} - Copied object.
*/
Utils.pickDeep = (o, paths) => {
if (Array.isArray(o)) {
return _.map(o, v=>
Utils.pickDeep(v, paths));
}
else if (null != o && 'object' === typeof o) {
const result = {};
for (const path of paths) {
const parts = path.split('.');
const part = parts.shift();
result[part] = o[part];
if (parts.length < 1) {
// do not recurse
}
else {
// recurse
result[part] = Utils.pickDeep(_.get(o, [part]), [parts.join('.')]);
}
}
return result;
}
else {
return o;
}
};
and
/**
* Like _.omit() but will also map over arrays implicitly.
* ie. path 'a.b.c' will transform {a:[{b:{c:1,d:2}}],e:4} => {a:[{b:{d:2}}],e:4}
*
* #param {object} o - Object to copy.
* #param {string[]} paths - List of paths to exclude.
* #returns {mixed} - Copied object.
*/
Utils.omitDeep = (o, paths) => {
if (Array.isArray(o)) {
return _.map(o, v=>
Utils.omitDeep(v, paths));
}
else if (null != o && 'object' === typeof o) {
const result = { ...o };
for (const path of paths) {
const parts = path.split('.');
const part = parts.shift();
delete result[part];
if (parts.length < 1) {
// do not recurse
}
else {
// recurse
result[part] = Utils.omitDeep(_.get(o, [part]), [parts.join('.')]);
}
}
return result;
}
else {
return o;
}
};

Compare two arrays and update with the new values by keeping the existing objects using javascript

Below are my two arrays .I want to compare them and the resultant array should contain the updated values.Id's are common..
The arrays spans to n levels ie., there is no fixed levels..
The first array ie., the array before updation..
var parentArray1=[
{
"id": 1,
"name": "test",
"context": [
{
"id": 1.1,
"name": "test 1.1"
}
]
},
{
"id": 2,
"name": "test"
},
{
"id": 3,
"name": "test",
"context": [
{
"id": 3.1,
"name": "test 3.1"
}
]
},
{
"id": 4,
"name": "test"
}
]
The operations that i performed are
1.Adding a new Item
2.Updating an existing item
As a result of these two operations the changed values I will be getting in a different array..
ie.,
var changedArray=
[
{
"id": 1,
"name": "test1",
"context": [
{
"id": 1.1,
"name": "Changed test 1.1"
}
]
},
{
"id": 5,
"name": "test5"
}
]
Now I have written a generic function that loops through the parentArray1 and using the unique propertiesI need to either add a new item,if the item is there in the changedArray or update an existing item at any level
The resultant array should be ..
[
{
"id": 1,
"name": "test",
"context": [
{
"id": 1.1,
"name": "Changed test 1.1"
}
]
},
{
"id": 2,
"name": "test"
},
{
"id": 3,
"name": "test",
"context": [
{
"id": 3.1,
"name": "test 3.1"
}
]
},
{
"id": 4,
"name": "test"
},
{
"id": 5,
"name": "test5"
}
]
Generic function:
compareArray(parentArray1, changedArray, ["id"]);
function compareArray(array1, array2, propertyArray) {
var newItem = new Array();
array2.map(function(a1Item) {
array1.map(function(a2Item) {
/ If array loop again /
if (a2Item.constructor === Array) {
compareArray(a2Item, a1Item)
} else {
/ loop the property name to validate /
propertyArray.map(function(property) {
if (a2Item[property]) {
if (a2Item[property] === a1Item[property]) {
a2Item = a1Item
} else {
var isAvailable = _.find(newItem, function(item) {
return item[property] === a1Item[property]
})
if (!isAvailable) {
newItem.push(a1Item);
}
}
}
})
}
});
});
/ Insert the new item into the source array /
newItem.map(function(item) {
array1.push(item);
});
console.log("After Compare : " + array1);
}
I suggest to use a temporary object for the reference to the id and update if exist or push if not exist.
var parentArray1 = [{ "id": 1, "name": "test", "context": [{ "id": 1.1, "name": "test 1.1" }] }, { "id": 2, "name": "test" }, { "id": 3, "name": "test", "context": [{ "id": 3.1, "name": "test 3.1" }] }, { "id": 4, "name": "test" }],
changedArray = [{ "id": 1, "name": "test1", "context": [{ "id": 1.1, "name": "Changed test 1.1" }] }, { "id": 5, "name": "test5" }];
function insert(array, data) {
function iter(array) {
array.forEach(function (a) {
if (!('id' in a)) {
return;
}
if (o[a.id] !== a) {
o[a.id] = a;
}
Object.keys(a).forEach(function (k) {
Array.isArray(a[k]) && iter(a[k]);
});
});
}
var o = {};
iter(array);
data.forEach(function (a) {
if (o[a.id]) {
Object.keys(a).forEach(function (k) {
o[a.id][k] = a[k];
});
return;
}
array.push(a);
});
}
insert(parentArray1, changedArray);
document.write('<pre>' + JSON.stringify(parentArray1, 0, 4) + '</pre>');
This is what I came up with:
function sameKeys(o1, o2, keys) {
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!o1.hasOwnProperty(key) || !o2.hasOwnProperty(key))
throw 'compared objects do not have the key ' + key;
if (o1[key] !== o2[key])
return false;
}
return true;
}
function isNothing(o) {
return typeof(o) === 'undefined' || o === null;
}
// this does not work if objects have functions as properties
function clone(o) {
if (isNothing(o))
return o;
return JSON.parse(JSON.stringify(o));
}
function extend(o1, o2, keys) {
if (isNothing(o2))
return;
if (isNothing(o1))
throw ('first parameter cannot be empty');
if (typeof(o1) != 'object' || typeof(o2) != 'object')
throw ('extend only works on objects');
Object.keys(o2).forEach(function (key) {
var newVal = o2[key];
if (o1.hasOwnProperty(key)) {
if (isNothing(newVal)) {
delete o1[key];
} else
if (Array.isArray(newVal)) {
compareArray(o1[key], newVal, keys);
} else {
switch (typeof(newVal)) {
case 'object':
extend(o1[key], newVal, keys);
break;
case 'boolean':
case 'number':
case 'string':
o1[key] = newVal;
break;
default:
throw 'not supported property type: ' + typeof(newVal);
}
}
} else {
o1[key] = clone(newVal);
}
});
}
function removeFromArray(arr, ids, keyArray) {
var indexes = [];
var it1s = arr.forEach(function (it, idx) {
if (sameKeys(ids, it, keyArray)) {
indexes.push(idx);
} else {
Object.keys(it).forEach(function (key) {
var newVal = it[key];
if (Array.isArray(newVal)) {
removeFromArray(it[key], ids, keyArray);
}
});
}
});
if (indexes.length) {
if (indexes.length > 1)
throw 'found multiple possible objects for the same key combination'
arr.splice(indexes[0], 1);
}
}
function compareArray(a1, a2, keyArray) {
a2.forEach(function (it2) {
var it1s = a1.filter(function (it) {
return sameKeys(it2, it, keyArray);
});
var it1;
if (!it1s.length) {
it1 = clone(it2);
a1.push(it1);
} else {
if (it1s.length > 1)
throw 'found multiple possible objects for the same key combination'
it1 = it1s[0];
extend(it1, it2, keyArray);
}
if (it2.removedIds) {
it2.removedIds.forEach(function (ids) {
removeFromArray(a1, ids, keyArray);
});
}
});
}
Use it with compareArray(parentArray1,changedArray,['id']);
Note that it would not work with objects that contain functions. Also, if the arrays would be large, perhaps a better solution is to sort both arrays by key, then always look from the last found object up. That's all I got for now.
Updated it with some concepts from Nina and some clearing of the code.
As I understood it, you only want to add properties. So extend({a: {b: 2}},{a:{c:3}}) will result in {a: {b:2,c:3}}. If this is not what you wanted, let me know.
I also added functionality for removing ids. If any of the objects in the array contains a removedIds array of the form [{id: 4},{id: 5}] then the items with those ids will be removed from the original array.
Slight modification on code, to satisfy your conditions. Try it!
function compareArray(originalArray, destinationArray, propertyArray) {
var newItem = new Array(), processedItem = new Array();
for (var i = 0; i < originalArray.length; i++) {
var sourceElement = originalArray[i];
for (var j = 0; j < destinationArray.length; j++) {
var destinationElement = destinationArray[j];
var isUpdated = false;
if (sourceElement.constructor === Array) {
compareArray(sourceElement, destinationElement, propertyArray);
} else {
/* loop the property name to validate */
propertyArray.map(function(property) {
if (sourceElement[property]) {
if (sourceElement[property] === destinationElement[property]) {
originalArray[i] = _.clone(destinationElement);
isUpdated = true;
return;
} else {
var isAvailable = _.find(newItem, function(item) {
return item[property] === destinationElement[property];
});
if (!isAvailable) {
var isAlreadyProcessed = _.find(processedItem, function(item) {
return item[property] === destinationElement[property];
});
if(!isAlreadyProcessed){
newItem.push(destinationElement);
}
}
}
}
});
}
if (isUpdated === true) {
break;
}
}
processedItem.push(sourceElement);
}
newItem.map(function(item) {
originalArray.push(item);
});
return originalArray;
}

Categories

Resources