array with objects transformation - javascript

I have an array with objects smth like this:
arr = [
{name: 'Igor', id: 1,....},
{name: 'Anton', id: 1,.... },
{name: 'Igor', id: 2,.... },
{name: 'Peter', id: 2,.... },
{name: 'Igor', id: 2,.... }
]
I need to get new array like:
arrId = [
{ id: 1, names: 'Igor, Anton' },
{ id: 2, names: 'Igor, Peter' }
]
can't think of good solution

In this example I use map and reduce.
function rearrange(arr) {
// use `reduce` to build an object using the ids as keys
// this allows us to place all the names with the same id together
// note we pass in an empty object to act as our initial p argument.
var out = arr.reduce(function (p, c) {
var key = c.id, name = c.name;
// if the key doesn't exist create it and set its value
// to an empty array
p[key] = p[key] || [];
// add the name to the array if it doesn't already exist
if (p[key].indexOf(name) === -1) p[key].push(name);
return p;
}, {});
// use `map` to return an array of objects
return Object.keys(out).map(function (el) {
// make sure we use an integer for the id, and
// join the array to get the appropriate output
return { id: +el, names: out[el].join(', ') };
});
}
rearrange(arr);
DEMO
OUTPUT
[
{
"id": 1,
"names": "Igor, Anton"
},
{
"id": 2,
"names": "Igor, Peter"
}
]

You can use reduce function
arr = [
{name: 'Igor', id: 1},
{name: 'Anton', id: 1 },
{name: 'Igor', id: 2 },
{name: 'Peter', id: 2 },
{name: 'Igor', id: 2 }
]
//first group needed fields from items by id
var result = arr.reduce(function(acc,cur){
if(!acc.map[cur.id]) {
acc.map[cur.id] = {id:cur.id, names:{}};
acc.result.push(acc.map[cur.id]);
}
acc.map[cur.id].names[cur.name]=true;
return acc;
},{map:{},result:[]})
//do addtional transfrom from map object with names, to string separated by comma
.result.map(function(el){
return {id:el.id, names: Object.keys(el.names).join(', ')};
});
console.log(result);
document.body.innerHTML = JSON.stringify(result,null, 2);

var arrId = [];
var arrKey = {};
for( var counter = 0; counter < arr.length; counter++)
{
var arrObj = arr[ counter ];
if ( !arrKey[ arrObj[ "id" ] ] )
{
arrKey[ arrObj[ "id" ] ] = [];
}
arrKey[ arrObj[ "id" ] ].push( arrObj[ "name" ] );
}
for ( var id in arrKey )
{
arrId.push( [ "id": id, names : arrKey[ id ].join( "," ) ] );
}
console.log(arrId );

Related

How to get array of objects only if array values matches with object array key

I have array arr1, arr2, arr3 and object array objarr
I would like to get array of objects based on condition
i.e
when passing arr1,
object array objarr should return only matched keys of arr1
i.e
when passing arr2,
object array objarr should return only matched keys of arr2 (if wrk is passed parameter only show that is true )
i.e
when passing arr3,
object array objarr should return only matched keys of arr3
var arr1=["name", "place"]
var arr2=["details", "wrk"]
var arr3=["name"]
var objarr=[
{id:1, name: "ayan", place: "MY", details: "finance" , wrk: true},
{id:2, name: "mike", place: "AU", details: "teaching", wrk: false },
{id:3, name: "john", place: "DE", details: "service" , wrk: true}
]
function outputData(arr1){
var final=[];
var result = objarr.map(e=>{
if(arr1.includes(Object.keys(e)){
final.push(e)
}
})
return final
;}
Expected Output;
//for arr1 is passed
[
{name: "ayan", place: "MY"},
{name: "mike", place: "AU"},
{name: "john", place: "DE"}
]
//for arr2 is passed
[
{details: "finance", wrk:true},
{details: "service", wrk: true}
]
//for arr3 is passed
[
{name: "ayan"},
{name: "mike"},
{name: "john"}
]
var arr1 = ["name", "place"];
var arr2 = ["details", "wrk"];
var arr3 = ["name"];
var objarr = [
{ id: 1, name: "ayan", place: "MY", details: "finance", wrk: true },
{ id: 2, name: "mike", place: "AU", details: "teaching", wrk: false },
{ id: 3, name: "john", place: "DE", details: "service", wrk: true }
]
function outputData(arr) {
var final = [];
objarr.forEach(obj => {
const temp = {};
arr.forEach(key => {
if(obj[key]){
temp[key]= obj[key];
}
});
if(Object.keys(temp).length===arr.length){
final.push(temp);}
});
return final;
}
console.log('arr1',outputData(arr1));
console.log('arr2',outputData(arr2));
console.log('arr3',outputData(arr3));
You can easily achieve the result using reduce and for..of loop
var arr1 = ["name", "place"];
var arr2 = ["details", "wrk"];
var arr3 = ["name"];
var objarr = [
{ id: 1, name: "ayan", place: "MY", details: "finance", wrk: true },
{ id: 2, name: "mike", place: "AU", details: "teaching", wrk: false },
{ id: 3, name: "john", place: "DE", details: "service", wrk: true },
];
function getData(target, source) {
return target.reduce((acc, curr) => {
const obj = {};
for (let prop of source) {
obj[prop] = curr[prop];
}
if (Object.keys(obj).includes("wrk")) {
if (obj["wrk"]) acc.push(obj);
} else acc.push(obj);
return acc;
}, []);
}
console.log(getData(objarr, arr1));
console.log(getData(objarr, arr2));
console.log(getData(objarr, arr3));
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try this solution:
function outputData(arr) {
return objarr.map(obj => Object.fromEntries(arr.map(k => [k, obj[k]])))
}
Since you need to select only specific nodes from objarr, going for Array.map willnot give the result. Use Some other looping logic for that.
I used Array.reduce here.
Logic
Loop through objarr with Array.reduce.
Loop through the paremeter array inside the reduce.
Check if the value for each node from the parameter array in the object in objarr array.
If the value is truthy, add that to an object.
If the length of keys of this object is same as the length of parameter array, push this object to accumulator.
Please Note: This logic checks truthy value for key "wrk" only. If you want to check for all boolean values, you can use the condition added as comment just above the if statement
var arr1 = ["name", "place"]
var arr2 = ["details", "wrk"]
var arr3 = ["name"]
var objarr = [
{ id: 1, name: "ayan", place: "MY", details: "finance", wrk: true },
{ id: 2, name: "mike", place: "AU", details: "teaching", wrk: false },
{ id: 3, name: "john", place: "DE", details: "service", wrk: true }
]
function outputData(arr) {
var result = objarr.reduce((acc, currNode) => {
const resp = {};
arr.forEach((option) => {
// Current condition is only for "wrk"
// To check truthy state for all boolean variables use the below condition
// ((typeof currNode[option] === "boolean" && currNode[option]) || typeof currNode[option] !== "boolean")
if ((option === "wrk" && currNode[option]) || option !== "wrk") {
resp[option] = currNode[option]
}
})
if(Object.keys(resp).length === arr.length) {
acc.push(resp)
}
return acc
}, []);
return result;
}
console.log(outputData(arr1));
console.log(outputData(arr2));
console.log(outputData(arr3));

i need a help how can i treat maximum call stack?

Thanks i fixed some sentence by advice. my code is like that,
i wanna find object with id. but if not, I want to return 'null'
function ha7(arr, id) { // i wanna find object with id
let result = [];
for(let i = 0 ; i < arr.length ; i++) {
if(arr[i].id === id) {
return arr[i] // found id, then return included object
}
else if(Array.isArray(arr[i].children)){ // but , its array
// let ar = ha7(arr[i].children, id)
result.push(...arr[i].children) // i put 'arr[i].children' to variables
}
}
if (result.id === id) {
return result // find object with id in inner
} else {
return ha7(result, id) // cant find. then go ahead!
}
return null // all of none exist id is return null
}
it is testing array.
let input = [
{
id: 1,
name: 'johnny',
},
{
id: 2,
name: 'ingi',
children: [
{
id: 3,
name: 'johnson',
},
{
id: 5,
name: 'steve',
children: [
{
id: 6,
name: 'lisa',
},
],
},
{
id: 11,
},
],
},
{
id: '13',
},
];
output = ha7(input, 5);
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }
output = ha7(input, 99);
console.log(output); // --> null
I tried a lot of trial, like that. i wanna know.
how can i treat maximum call stack ?
and i wanna return 'null' value.
function ha7(arr, id) { // i wanna find object with id
let result = [];
for(let i = 0 ; i < arr.length ; i++) {
if(arr[i].id === id) {
return arr[i] // found id, then return included object
}
else if(Array.isArray(arr[i].children)){ // but , its array
// let ar = ha7(arr[i].children, id)
result.push(...arr[i].children) // i put 'arr[i].children' to variables
}
}
if (result.id === id) {
return result // find object with id in inner
} else {
return ha7(result, id) // cant find. then go ahead!
}
return null // all of none exist id is return null
}
let input = [
{
id: 1,
name: 'johnny',
},
{
id: 2,
name: 'ingi',
children: [
{
id: 3,
name: 'johnson',
},
{
id: 5,
name: 'steve',
children: [
{
id: 6,
name: 'lisa',
},
],
},
{
id: 11,
},
],
},
{
id: '13',
},
];
output = ha7(input, 5);
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }
output = ha7(input, 99);
console.log(output); // --> null
This code is the problem:
if (result.id === id) {
return result // find object with id in inner
} else {
return ha7(result, id) // cant find. then go ahead!
}
Two lines above this you initialize result as an array. Then in this conditional test you treat the array result as if it were an object. So, since result.id does not equal id, the else condition recurses for ever and ever.
I've taken a different, more functional approach to the task.
filter the array on the id
If there is a length then at least one was found
Return the first one
Next filter out all the objects with children
Then create an array (with .map() that only includes the children
This will create an array of arrays, so must flatten it
If there are no children, then id was not found
Return null
Recurse the children
let input=[{id:1,name:"johnny"},{id:2,name:"ingi",children:[{id:3,name:"johnson"},{id:5,name:"steve",children:[{id:6,name:"lisa"}]},{id:11}]},{id:"13"}];
function ha7(arr, id) {
let found = arr.filter(o => o.id === id);
if (found.length) return found[0]; // return first match
let children = arr.filter(o=>!!o.children).map(c=>c.children).flat();
if(!children.length) return null;
return ha7(children, id);
}
output = ha7(input, 5);
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }
output = ha7(input, 99);
console.log(output); // --> null

How to find a value of array inside another array then set data accordingly?

I have this first array:
const arrayOne = [
{number: "DS5.11", name: "nameOne"},
{number: "DS5.11", name: "nameTwo"},
{number: "D5.10", name: "NameThree"}
]
Then I have this second array. Similar to the first one:
const arrayOne = [
{number: "DS5.11", name: "nameFour"},
{number: "DS5.85", name: "nameFive"},
{number: "D5SA1", name: "NameSix"}
]
As you can see, the first and second number values of the first array are equal to the same first number value of the second array. So what I've been trying to do is to filter the first array, check the number, and basically, if there's a number from the first array that it's repeated on the second array, a new object is created.
Something like:
let arrayThree = []
arrayOne.filter((e,i) => {
arrayTwo.map((obj,idx) => { if(obj.number === e.number) {
arrayThree[i] = {key: value}
}})
})
Also, I want this new object to have as many index numbers as there are matches between the first and the second array.
What am I doing wrong? Thanks in advance.
I don't know if I understood correctly, but here's my solution:
arrayOne.filter(obj1 => {
for (obj2 of arrayTwo) {
if (obj1.number === obj2.number) {
arrayThree.push({ key: obj1.number });
return false;
} else {
return true;
}
}
});
This method filters ArrayOne values, and save the repeated numbers inside arrayThree.
ArrayTwo keeps untouched.
If you use lodash the following code should work
const arrayOne = [
{number: "DS5.11", name: "nameOne"},
{number: "DS5.11", name: "nameTwo"},
{number: "D5.10", name: "NameThree"}
]
const arrayTwo = [
{number: "DS5.11", name: "nameFour"},
{number: "DS5.85", name: "nameFive"},
{number: "D5SA1", name: "NameSix"}
]
console.log(_.intersectionBy(arrayOne,arrayTwo,"number"))
if you want to save the name property of arrayOne then
let arrayThree = [];
arrayOne.filter((e,i) => {
arrayTwo.map((obj,idx) => { if(obj.number === e.number) {
arrayThree[i] = e;
}})
})
similarly to save the name property of arrayTwo
let arrayThree = [];
arrayOne.filter((e,i) => {
arrayTwo.map((obj,idx) => { if(obj.number === e.number) {
arrayThree[i] = obj;
}})
})
Create a map of number to array of elements using array 1
const intersectionMap = arrayOne.reduce(
(obj, el) => {
if (!obj[el.number]) obj[el.number] = [];
obj[el.number].push(el)
return obj;
},
{}
);
Iterate over array 2 and if intersection map has key, push element into array
arrayTwo.forEach(el => obj[el.number]?.push(el));
Filter out arrays with length 1 and flatten result arrays
const res = Object.values(obj).flatMap(arr => arr.length > 1 ? arr : []);
No nested loops so O(n) runtime complexity.
const arrayOne = [
{ number: "DS5.11", name: "nameOne" },
{ number: "DS5.11", name: "nameTwo" },
{ number: "D5.10", name: "NameThree" }
];
const arrayTwo = [
{ number: "DS5.11", name: "nameFour" },
{ number: "DS5.85", name: "nameFive" },
{ number: "D5SA1", name: "NameSix" }
];
const intersectionMap = arrayOne.reduce(
(obj, el) => {
if (!obj[el.number]) obj[el.number] = [];
obj[el.number].push(el)
return obj;
},
{}
);
arrayTwo.forEach(el => intersectionMap[el.number]?.push(el));
const res = Object.values(intersectionMap).flatMap(arr => arr.length > 1 ? arr : []);
console.log(res);

Array.filter() to remove duplicate Ojects

I would like to fuse Array.filter() function to remove duplicate objects
I am able to achieve in the case of string or integer arrays. But I am not able to achieve the same with array of objects as in the second case of names
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let x = names => names.filter((v, i, arr) => arr.indexOf(v) === i);
console.log(x(names)); //[ 'John', 'Paul', 'George', 'Ringo' ]
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" } ];
// returns the same original array
Could you please help?
Using Array#reduce() and a Map accumulator then spread the values() of the Map into array
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" } ];
const unique = [... names.reduce((a,c)=>(a.set(c.name,c)),new Map).values()]
console.log(unique)
Use Array.reduce and Object.values
Iterate over the array and create an object with key as name and value as object from array. In case of objects with same name, the value will be overwritten in resultant object. Finally use Object.values to collect all the unique objects.
const names = [{ name: "John" },{ name: "Paul" },{ name: "George" },{ name: "Ringo" },{ name: "John" } ];
let result = Object.values(names.reduce((a,c) => Object.assign(a, {[c.name]:c}),{}));
console.log(result);
For tweaking - Plunker
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" }
];
/* unique => Filter: Remove all duplicate items from an array. Works with plain objects as well, since we stringify each array item.
* #type public Function
* #name unique
* #return Function( item )
* #notes
*/
const unique = () => {
const seen = {};
return item => {
const json = JSON.stringify( item );
return seen.hasOwnProperty( json )
? false
: ( seen[ json ] = true );
};
};
const result = names.filter( unique() );
console.log( result );
You could use lodash's _uniqBy for this:
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" } ];
const result = _uniqBy(names, 'name');
This can be done with the help of Sets as well
var names = [{ name: "John" },{ name: "Paul" },{ name: "George" },{ name: "Ringo" },{ name: "John" } ];
var result = Array.from(
names.reduce((s, d) => s.add(d.name), new Set)
, d => ({ name: d })
)
console.log(result)
Keith had a great suggestion to use findIndex with filter instead of indexOf. Object literals are always unique references, so we cannot compare them. We can however compare the name keys between the objects. We can do this with the aforementioned functions.
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" }
];
console.log(names.filter(({name1}, i, a) => {
return i == a.findIndex(({name2}) => {
return name1 == name2;
});
});
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
function removeDups(names) {
let unique = {};
names.forEach(function(i) {
if(!unique[i]) {
unique[i] = true;
}
});
return Object.keys(unique);
}
removeDups(names); //'John', 'Paul', 'George', 'Ringo'

JavaScript Object Array: Removing objects with duplicate properties

I have an array of objects:
[
{ id: 1, name: "Bob" },
{ id: 1, name: "Donald" },
{ id: 2, name: "Daryl" }
]
I'd like to strip out objects with duplicate Ids, leaving an array that would look like this:
[
{ id: 1, name: "Bob" },
{ id: 2, name: "Daryl" }
]
I don't care which objects are left, as long as each ID is unique. Anything in Underscore, maybe, that would do this?
Edit: This is not the same as the duplicate listed below; I'm not trying to filter duplicate OBJECTS, but objects that contain identical IDs. I've done this using Underscore - I'll post the answer shortly.
You can use reduce and some to good effect here:
var out = arr.reduce(function (p, c) {
// if the next object's id is not found in the output array
// push the object into the output array
if (!p.some(function (el) { return el.id === c.id; })) p.push(c);
return p;
}, []);
DEMO
the es6 way
function removeDuplicates(myArr, prop) {
return myArr.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos
})
}
Test it
let a =[
{ id: 1, name: "Bob" },
{ id: 1, name: "Donald" },
{ id: 2, name: "Daryl" }
]
console.log( removeDuplicates( a, 'id' ) )
//output [
{ id: 1, name: "Bob" },
{ id: 2, name: "Daryl" }
]
If you use underscore, you can use the _uniq method
var data = [
{ id: 1, name: "Bob" },
{ id: 1, name: "Donald" },
{ id: 2, name: "Daryl" }
]
_.uniq(data, function(d){ return d.ID });
Produces a duplicate-free version of the array, using === to test object equality. In particular only the first occurence of each value is kept. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iteratee function.
Source: http://underscorejs.org/#uniq
Can use es6 Map collection mix with reduce
const items = [
{ id: 1, name: "Bob" },
{ id: 1, name: "Donald" },
{ id: 2, name: "Daryl" }
]
const uniqItems = [...items.reduce((itemsMap, item) =>
itemsMap.has(item.id) ? itemsMap : itemsMap.set(item.id, item)
, new Map()).values()]
console.log(uniqItems);
Using findIndex should be the simplest solution.
array.filter((elem, index, arr) => arr.findIndex(e => e.id === elem.id) === index)
You can simply filter the array, but you'll need an index of existing IDs that you've already used...
var ids = [];
var ar = [
{ id: 1, name: "Bob" },
{ id: 1, name: "Donald" },
{ id: 2, name: "Daryl" }
];
ar = ar.filter(function(o) {
if (ids.indexOf(o.id) !== -1) return false;
ids.push(o.id);
return true;
});
console.log(ar);
Here's some documentation on filter()...
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Categories

Resources