Mark only second duplicated object in js array - javascript

I have a list of objects:
[{name: 'Elza'}, {name: 'Tom'}, {name: 'Elza'}]
I use the below methods to get duplicated objects(by name) and assign a prop isDuplicated:
const duplicatedNames = arrayOfObjects
.map(e => e['name'])
.map((e, i, final) => final.indexOf(e) !== i && i++)
.filter(obj => arrayOfObjects[obj])
.map(e => !arrayOfObjects[e]['name']);
const result = arrayOfObjects.filter((obj, i) => {
return duplicatedNames.includes(obj.name) && Object.assign(obj, { isDuplicated: true });
});
I receive an array like:
[{name: 'Elza', isDuplicated: true}, {name: 'Tom'}, {name: 'Elza', isDuplicated: true}]
I would like to mark only the second occurrence of duplicate- so i would like the result to be:
[{name: 'Elza'}, {name: 'Tom'}, {name: 'Elza', isDuplicated: true}]
Can anyone know how to do it base on my code?

Here is a function that checks if a name exist more then once.
let data = [{name:'Elza'}, {name:'Tom'}, {name:'Elza'}, {name: "Jerry"}, {name: "Jerry"}];
function checkDup(arr){
let cache = [];
return arr.map(({name}, index) => {
if(!cache.find(el => el.name == name)){
cache.push({name, index});
return {name, index};
}
let { index: cacheIndex } = cache.find(el => el.name === name);
return {name,index: cacheIndex , isDuplicated: true};
})
}
console.log(checkDup(data));

You could create a Set of names. If the size of the set is same as after the name has been added, then it's duplicate record.
const input = [{name:'Elza'}, {name:'Tom'}, {name:'Elza'}],
names = new Set;
for (const o of input)
if (names.size === names.add(o.name).size)
o.isDuplicate = true
console.log(input)

You can try this:
let users = [{name:'Elza'}, {name:'Tom'}, {name:'Elza'}]
let flags = [], output = [];
users.forEach(user => {
if (flags[user.name]) {
output.forEach(item => {
if (item.name === user.name) {
item.isDuplicated = true
output.push(user);
}
})
} else {
flags[user.name] = true;
output.push(user);
}
})

Given your original array A, you could create a temporary array B and, for each a element of A, check:
if B contains a.name, then set a.isDuplicated to true;
else, push a.name in B.
let A = [{name: 'Elza'}, {name: 'Tom'}, {name: 'Elza'}];
let B = [];
A.forEach(a => {
if (B.includes(a.name)) {
a.isDuplicated = true;
} else {
B.push(a.name);
}
});
console.log(A);

You can use reduce with a helper object:
const collection = [{ name: 'Elza'}, { name: 'Tom'}, { name: 'Elza' }]
const helper = {}
const result = collection.reduce((acc, { name }) => {
if (helper[name]) {
return [...acc, { name, isDuplicate: true }]
}
helper[name] = 'x';
return [...acc, { name }]
}, [])
console.log(result)

Related

how to create nested object from an array with same key (javascript)

arr = ["sadik", "arif", "rahman"]
I want to create a nested object with the same key but different value like:
{
subcategory: {
name: sadik
subcategory: {
name: arif
subcategory: {
name: rahman
}
}
}
}
my code:
let arr = ['sadik', 'arif', 'babor']
let obj = {}
arr.forEach((elem) => {
let a = {}
a["subcategory"] = {name:elem}
Object.assign(obj, a)
})
i get only last value:
{
subcategory: {
name:"babor"
}
}
Your code did not work because you were replacing your "subcategory" key value in each iteration. You have change the reference object to the next nested level each time to get the expected output, see the working snippet below:
const arr = ['level-1', 'level-2', 'level-3']
const obj = {}
let refObj = obj;
arr.forEach( ele => {
refObj = refObj['subcategory'] || refObj;
refObj['subcategory'] = { 'name': ele};
})
console.log('output', obj);

Cannot access value of object property

I am looping though an object to retrieve some properties but for some reason I cannot access the value of a nested object property.
This is my looping function:
parseContacts = (contacts) => {
return contacts.map(contact => {
let parsedContact = {};
Object.keys(contact).forEach(key => {
if (key === 'givenName') {
parsedContact.firstName = contact[key];
} if (key === 'familyName') {
parsedContact.surname = contact[key];
} if (key === 'phoneNumbers') {
parsedContact.phoneNumber = contact[key][0].number;
}
})
return parsedContact;
})
}
firstName and surname work fine, but in the last if statement I get undefined. the property with key phoneNumbers it is an array of objects, and this is item 0 in the array:
{id: "302", label: "mobile", number: "+44 7X7X 50XX72"}
When I use this code instead:
} if (key === 'phoneNumbers') {
parsedContact.phoneNumber = contact[key][0];
}
without .number on the end I get the whole object back fine, I just can't get back only the number property from the object.
Update
On closer inspection, the array which has over 800 large objects in, some of the lengths of the phoneNumbers arrays were 0 so the property did not exist. This was causing the whole function to fail. Thanks for the comments below.
My solution was to add to the if statement:
if (key === 'phoneNumbers' && contact[key].length)
You can prevent the empty phoneNumbers array issue like this:
contact[key] && contact[key].length ? contact[key][0].number : ''
const parseContacts = contacts => {
return contacts.map(contact => {
let parsedContact = {}
Object.keys(contact).forEach(key => {
switch (key) {
case 'givenName':
parsedContact.firstName = contact[key]
break
case 'familyName':
parsedContact.surname = contact[key]
break
case 'phoneNumbers':
parsedContact.phoneNumber = contact[key] && contact[key].length ? contact[key][0].number : ''
}
})
return parsedContact
})
}
const contacts = []
for (let i = 0; i < 10; i++) {
contacts.push({
givenName: faker.name.firstName(),
familyName: faker.name.lastName(),
phoneNumbers: [
{
id: faker.random.uuid(),
label: 'mobile',
number: faker.phone.phoneNumber()
}, {
id: faker.random.uuid(),
label: 'mobile',
number: faker.phone.phoneNumber()
}
]
})
}
contacts.push({
givenName: faker.name.firstName(),
familyName: faker.name.lastName(),
phoneNumbers: []
})
contacts.push({
givenName: faker.name.firstName(),
familyName: faker.name.lastName(),
phoneNumbers: null
})
console.log('RESULT ' + JSON.stringify(parseContacts(contacts)))
<script src="https://rawgit.com/Marak/faker.js/master/examples/browser/js/faker.js"></script>

Add values of elements in array using javascript with out using const

I had an array with
0:{Name: "Test1",Cost:100,Revenue:200}
1:{Name: "Test2",Cost:100,Revenue:100}
2:{Name: "Test3",Cost:100,Revenue:300}
3:{Name: "Test2",Cost:200,Revenue:100}
4:{Name: "Test1",Cost:100,Revenue:300}
5:{Name: "Test4",Cost:100,Revenue:300}
I am expecting result with out duplicates based on names and other values (cost and revenue) as added
0:{Name: "Test1",Cost:200,Revenue:500}
1:{Name: "Test2",Cost:300,Revenue:200}
2:{Name: "Test3",Cost:100,Revenue:300}
3:{Name: "Test4",Cost:100,Revenue:300}
I am trying with below code
var removeDuplicates = function(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for(var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for(i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}
console.log('Name array ',removeDuplicates(tempRoleMap, 'Name'));
Can you help me out
You can use reduce to iterate over the array while assigning to an accumulator object indexed by Name. On each array item, either assign it as the new object at that key in the accumulator if it doesn't exist yet, or iterate over the item's entries (other than Name) and add to the appropriate property in the accumulator.
At the end, you'll have an object indexed by Names, so to get the array out of the object, call Object.values on it:
const arr=[{Name:"Test1",Cost:100,Revenue:200},{Name:"Test2",Cost:100,Revenue:100},{Name:"Test3",Cost:100,Revenue:300},{Name:"Test2",Cost:200,Revenue:100},{Name:"Test1",Cost:100,Revenue:300},{Name:"Test4",Cost:100,Revenue:300}]
const result = Object.values(arr.reduce((a, { Name, ...rest }) => {
if (!a[Name]) a[Name] = { Name, ...rest };
else {
Object.entries(rest).forEach(([key, val]) => {
a[Name][key] += val;
});
}
return a;
}, {}));
console.log(result);
It's best to use ES6+ when writing code, and then you can transpile it down to ES5 automatically with Babel and polyfills. But, if you had to write in ES5, then:
var arr=[{Name:"Test1",Cost:100,Revenue:200},{Name:"Test2",Cost:100,Revenue:100},{Name:"Test3",Cost:100,Revenue:300},{Name:"Test2",Cost:200,Revenue:100},{Name:"Test1",Cost:100,Revenue:300},{Name:"Test4",Cost:100,Revenue:300}]
var obj = arr.reduce(function(a, item) {
var Name = item.Name;
if (!a[Name]) a[Name] = item;
else {
Object.keys(item).forEach(function(key) {
if (key === 'Name') return;
a[Name][key] += item[key];
});
}
return a;
}, {});
var output = [];
Object.keys(obj).forEach(function(key) {
output.push(obj[key]);
});
console.log(output);
As you can see, it's a whole lot wordier and inelegant - better to write in the latest and greatest version of the language, and transpile down to your target environment automatically later.
You can get the desired output using .reduce() and Object.values():
let data = [
{Name: "Test1", Cost:100, Revenue:200},
{Name: "Test2", Cost:100, Revenue:100},
{Name: "Test3", Cost:100, Revenue:300},
{Name: "Test2", Cost:200, Revenue:100},
{Name: "Test1", Cost:100, Revenue:300},
{Name: "Test4", Cost:100, Revenue:300}
];
let result = Object.values(
data.reduce((r, c) => {
r[c.Name] = r[c.Name] || {};
r[c.Name].Cost = (r[c.Name].Cost || 0) + c.Cost;
r[c.Name].Revenue = (r[c.Name].Revenue || 0) + c.Revenue;
return r;
}, {})
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You will find a solution in the snippet below:
var arr = [{Name: "Test1",Cost:100,Revenue:200},
{Name: "Test2",Cost:100,Revenue:100},
{Name: "Test3",Cost:100,Revenue:300},
{Name: "Test2",Cost:200,Revenue:100},
{Name: "Test1",Cost:100,Revenue:300},
{Name: "Test4",Cost:100,Revenue:300}];
function removeDuplicates(arr, prop){
arr.forEach((a, i) => arr.slice(i+1).filter(e => e[prop] == a[prop]).forEach( el => {
if (arr.indexOf(el) > -1)
arr.splice(arr.indexOf(el), 1);
}));
return arr;
}
console.log(removeDuplicates(arr, 'Name'));

Split an object in to array of objects based on similar values

var pr = {
name: "ball",
race: "ball",
weapon: "axe",
};
var save=new Object;
var keys=Object.keys(pr);
for(var k in pr) {
}
console.log(save); // should end up with {name:'ball',race:'ball'}
If I have understood the question correctly, one option is:
const keys = Object.keys(pr);
const ret = keys.reduce((ret, k, i) => {
const f = keys.find((k2, i2) => i !== i2 && pr[k] === pr[k2]);
if (f) ret[k] = pr[k];
return ret;
}, {});
Here is what I came up with.
var pr = {
name: "ball",
race: "ball",
weapon: "axe"
};
const dupValues = Object.values(pr).reduce(
(acc, cur) => ({ ...acc, [cur]: (acc[cur] || 0) + 1 }),
{}
);
const result = Object.keys(pr)
.filter(key => dupValues[pr[key]] > 1)
.reduce((acc, curr) => ({ ...acc, [curr]: pr[curr] }), {});
console.log(result);
// {name:'ball',race:'ball'}
One way to do it is use the save object as a histogram, keeping track of duplicates. Then, filter out any keys with 0 count using reduce. This should have better performance than a linear function like find:
var pr = {
name: "ball",
race: "ball",
weapon: "axe"
};
var save = {};
for (var k in pr) {
save[pr[k]] = pr[k] in save ? save[pr[k]] + 1 : 0;
}
var result = Object.keys(pr).reduce((a, e) => {
if (save[pr[e]]) { a[e] = pr[e]; }
return a;
}, {});
console.log(result);
It works. Simple and clear. References : Array.reduce()
Iterate through each key value pair, and accumulate the result until the loop ends.
var pr = {
name: "ball",
race: "ball",
weapon: "axe",
item:"bat",
newitem:"bat",
newweapon: "axe"
};
var result = Object.keys(pr).reduce(function(acc, key){
var ispresent = false;
acc.forEach(function(obj,i){
if(ispresent) return;
if(Object.values(obj)[0]===pr[key])
{
obj[key]=pr[key];
ispresent = true;
}
});
if(!ispresent)
{
var newobj = {};newobj[key]=pr[key];
acc.push(newobj)
}
return acc;
},[])
console.log(result)

Removing object properties with Lodash

I have to remove unwanted object properties that do not match my model. How can I achieve it with Lodash?
My model is:
var model = {
fname: null,
lname: null
}
My controller output before sending to the server will be:
var credentials = {
fname: "xyz",
lname: "abc",
age: 23
}
I am aware I can use
delete credentials.age
but what if I have lots of unwanted properties? Can I achieve it with Lodash?
You can approach it from either an "allow list" or a "block list" way:
// Block list
// Remove the values you don't want
var result = _.omit(credentials, ['age']);
// Allow list
// Only allow certain values
var result = _.pick(credentials, ['fname', 'lname']);
If it's reusable business logic, you can partial it out as well:
// Partial out a "block list" version
var clean = _.partial(_.omit, _, ['age']);
// and later
var result = clean(credentials);
Note that Lodash 5 will drop support for omit
A similar approach can be achieved without Lodash:
const transform = (obj, predicate) => {
return Object.keys(obj).reduce((memo, key) => {
if(predicate(obj[key], key)) {
memo[key] = obj[key]
}
return memo
}, {})
}
const omit = (obj, items) => transform(obj, (value, key) => !items.includes(key))
const pick = (obj, items) => transform(obj, (value, key) => items.includes(key))
// Partials
// Lazy clean
const cleanL = (obj) => omit(obj, ['age'])
// Guarded clean
const cleanG = (obj) => pick(obj, ['fname', 'lname'])
// "App"
const credentials = {
fname:"xyz",
lname:"abc",
age:23
}
const omitted = omit(credentials, ['age'])
const picked = pick(credentials, ['age'])
const cleanedL = cleanL(credentials)
const cleanedG = cleanG(credentials)
Get a list of properties from model using _.keys(), and use _.pick() to extract the properties from credentials to a new object:
var model = {
fname:null,
lname:null
};
var credentials = {
fname:"xyz",
lname:"abc",
age:23
};
var result = _.pick(credentials, _.keys(model));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>
If you don't want to use Lodash, you can use Object.keys(), and Array.prototype.reduce():
var model = {
fname:null,
lname:null
};
var credentials = {
fname:"xyz",
lname:"abc",
age:23
};
var result = Object.keys(model).reduce(function(obj, key) {
obj[key] = credentials[key];
return obj;
}, {});
console.log(result);
You can easily do this using _.pick:
var model = {
fname: null,
lname: null
};
var credentials = {
fname: 'abc',
lname: 'xyz',
age: 2
};
var result = _.pick(credentials, _.keys(model));
console.log('result =', result);
<script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>
But you can simply use pure JavaScript (specially if you use ECMAScript 6), like this:
const model = {
fname: null,
lname: null
};
const credentials = {
fname: 'abc',
lname: 'xyz',
age: 2
};
const newModel = {};
Object.keys(model).forEach(key => newModel[key] = credentials[key]);
console.log('newModel =', newModel);
Lodash unset is suitable for removing a few unwanted keys.
const myObj = {
keyOne: "hello",
keyTwo: "world"
}
unset(myObj, "keyTwo");
console.log(myObj); /// myObj = { keyOne: "hello" }
Here I have used omit() for the respective 'key' which you want to remove... by using the Lodash library:
var credentials = [{
fname: "xyz",
lname: "abc",
age: 23
}]
let result = _.map(credentials, object => {
return _.omit(object, ['fname', 'lname'])
})
console.log('result', result)
You can use _.omit() for emitting the key from a JSON array if you have fewer objects:
_.forEach(data, (d) => {
_.omit(d, ['keyToEmit1', 'keyToEmit2'])
});
If you have more objects, you can use the reverse of it which is _.pick():
_.forEach(data, (d) => {
_.pick(d, ['keyToPick1', 'keyToPick2'])
});
To select (or remove) object properties that satisfy a given condition deeply, you can use something like this:
function pickByDeep(object, condition, arraysToo=false) {
return _.transform(object, (acc, val, key) => {
if (_.isPlainObject(val) || arraysToo && _.isArray(val)) {
acc[key] = pickByDeep(val, condition, arraysToo);
} else if (condition(val, key, object)) {
acc[key] = val;
}
});
}
https://codepen.io/aercolino/pen/MWgjyjm
This is my solution to deep remove empty properties with Lodash:
const compactDeep = obj => {
const emptyFields = [];
function calculateEmpty(prefix, source) {
_.each(source, (val, key) => {
if (_.isObject(val) && !_.isEmpty(val)) {
calculateEmpty(`${prefix}${key}.`, val);
} else if ((!_.isBoolean(val) && !_.isNumber(val) && !val) || (_.isObject(val) && _.isEmpty(val))) {
emptyFields.push(`${prefix}${key}`);
}
});
}
calculateEmpty('', obj);
return _.omit(obj, emptyFields);
};
For array of objects
model = _.filter(model, a => {
if (!a.age) { return a }
})
Recursively removing paths.
I just needed something similar, not removing just keys, but keys by with paths recursively.
Thought I'd share.
Simple readable example, no dependencies
/**
* Removes path from an object recursively.
* A full path to the key is not required.
* The original object is not modified.
*
* Example:
* const original = { a: { b: { c: 'value' } }, c: 'value' }
*
* omitPathRecursively(original, 'a') // outputs: { c: 'value' }
* omitPathRecursively(original, 'c') // outputs: { a: { b: {} } }
* omitPathRecursively(original, 'b.c') // { a: { b: {} }, c: 'value' }
*/
export const omitPathRecursively = (original, path, depth = 1) => {
const segments = path.split('.')
const final = depth === segments.length
return JSON.parse(
JSON.stringify(original, (key, value) => {
const match = key === segments[depth - 1]
if (!match) return value
if (!final) return omitPathRecursively(value, path, depth + 1)
return undefined
})
)
}
Working example: https://jsfiddle.net/webbertakken/60thvguc/1/
While looking for a solution that would work for both arrays and objects, I didn't find one and so I created it.
/**
* Recursively ignore keys from array or object
*/
const ignoreKeysRecursively = (obj, keys = []) => {
const keyIsToIgnore = (key) => {
return keys.map((a) => a.toLowerCase()).includes(key)
}
const serializeObject = (item) => {
return Object.fromEntries(
Object.entries(item)
.filter(([key, value]) => key && value)
.reduce((prev, curr, currIndex) => {
if (!keyIsToIgnore(curr[0]))
prev[currIndex] =
[
curr[0],
// serialize array
Array.isArray(curr[1])
? // eslint-disable-next-line
serializeArray(curr[1])
: // serialize object
!Array.isArray(curr[1]) && typeof curr[1] === 'object'
? serializeObject(curr[1])
: curr[1],
] || []
return prev
}, []),
)
}
const serializeArray = (item) => {
const serialized = []
for (const entry of item) {
if (typeof entry === 'string') serialized.push(entry)
if (typeof entry === 'object' && !Array.isArray(entry)) serialized.push(serializeObject(entry))
if (Array.isArray(entry)) serialized.push(serializeArray(entry))
}
return serialized
}
if (Array.isArray(obj)) return serializeArray(obj)
return serializeObject(obj)
}
// usage
const refObject = [{name: "Jessica", password: "ygd6g46"}]
// ignore password
const obj = ignoreKeysRecursively(refObject, ["password"])
// expects returned array to only have name attribute
console.log(obj)
let asdf = [{"asd": 12, "asdf": 123}, {"asd": 121, "asdf": 1231}, {"asd": 142, "asdf": 1243}]
asdf = _.map(asdf, function (row) {
return _.omit(row, ['asd'])
})

Categories

Resources