Why isn't omit() removing the undefined values after mapping? - javascript

I'm using the following Lodash chained utilities to map/flat and array and produce a new one while excluding the undefined valued.
const array = _(resp.data)
.omit(_.isUndefined)
.flatMap('building')
.value()
console.log(array)
And this is the result:
As you can see the undefined values are still being included. Why is this?
EDIT:
resp.data looks like this
[
{ username: '', building: [ name: '' ] }
{ username: '', building: [ name: '' ] }
// etc...
]
EDIT2:
Those undefined values are probably the empty building that come objects have.

The extra undefined value should belong to a user in the resp.data where no building exist. All you have to do is to _.filter() all values that are undefined through _.identity() after the _.flatMap().
Note: Using _.omit should only be used against objects, using this in the context of an array (e.g. _.flatMap() which results in an array) would yield a result of an object wherein the index of each item is it's index in the array. You should use _.filter() instead.
var data = [
{ username: 'user1', building: [ { name: 'building1' } ] },
{ username: 'user2', building: [ { name: 'building2' } ] },
{ username: 'user3' }
];
var result = _(data)
.flatMap('building')
.filter(_.identity)
.value();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>

Try this.
const array = _(resp.data)
.omit(_.isUndefined)
.flatMap('building')
.map()
.omit(_.isUndefined)
.value()

Related

Remove element from objects in javascript multidimensional array

I have read several solutions to this problem here. When I try it, I continue to receive an error for the pop() method.
I have what is essentially a multidimensional array in javascript.
I am tasked with returning the array with the sensitive info removed (e.g. remove the SSN, in this example)
I thought I could use a foreach loop, and the pop() function to remove the last element of the child arrays, the SSN.
testing it using node on the commandline, the stdout is telling me that element.pop() is not a function. i've tried it with pop(), slice(), filter(), all with no success.
when running $> node filename.js
H:\Apache2\htdocs\test\filename.js:50
noppi[i] = element.pop();
^
TypeError: element.pop is not a function
let recs = [
{
ID: 1,
NAME: 'John',
EMAIL: 'john#example.com',
SSN: '123'
}, {
ID: 2,
NAME: 'Sally',
EMAIL: 'sally#example.com',
SSN: '456'
}, {
ID: 3,
NAME: 'Angie',
EMAIL: 'angie#example.com',
SSN: '789'
}
];
let i = 0;
let noppi = [];
recs.forEach(element => {
noppi[i] = element.pop();
i++;
});
console.log(noppi);
At the risk of sounding redundant, I'll briefly reiterate what the earlier answers have already stated.
The input data structure isn't a multi-dimensional array [ [ ... ], [ ... ] ] , it's an array of objects [ {...}, {...} ]. So you can't use Array methods like .pop() on the objects {...}.
Here's a simple one-liner that uses .forEach() and delete.
recs.forEach(obj => delete obj.SSN)
delete is an operator with one purpose: to remove an object's property like for example SSN: '123-45-6789'. Simple and perfect.
Note, .forEach() mutates the array, meaning that it's the original data being changed (see Minja's comment).
let recs = [
{
ID: 1,
NAME: 'John',
EMAIL: 'john#example.com',
SSN: '123'
}, {
ID: 2,
NAME: 'Sally',
EMAIL: 'sally#example.com',
SSN: '456'
}, {
ID: 3,
NAME: 'Angie',
EMAIL: 'angie#example.com',
SSN: '789'
}
];
recs.forEach(obj => delete obj.SSN);
console.log(recs)
Try this:
recs.forEach(element => {
noppi.push = element;
});
You are trying to use pop() on an object not an array
As per your need you need to remove SSN from your object, try below code it should work for you.
recs.forEach(element => {
const { SSN, ...rest } = element;
noppi.push(rest);
});
Here we are removing SSN from object and rest will push in noppi.

I have array with objects, I need to fetch all the objects that matches the value of userId starts with 'US' [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed last year.
Below is the array with objects:
myArray:[
{"name":"Ram", "email":"ram#gmail.com", "userId":"HB000006"},
{"name":"Shyam", "email":"shyam23#gmail.com", "userId":"US000026"},
{"name":"John", "email":"john#gmail.com", "userId":"HB000011"},
{"name":"Bob", "email":"bob32#gmail.com", "userId":"US000106"}
]}
I tried this but I am not getting output:
item= myArray.filter(element => element.includes("US"));
I am new to Angular.
let filteredArray = myArray.filter(function (item){
return item.userId.substring(0,2).includes('US')
})
Console.log(filteredArray)
//Output
[ { name: 'Shyam', email: 'shyam23#gmail.com', userId: 'US000026' },
{ name: 'Bob', email: 'bob32#gmail.com', userId: 'US000106' } ]
As noted by #halfer - You need to filter on the property that you are interested in - in this case - 'userId' - you can do this by simply adding the property into the code you already had tried and it will log out the specified items - or alternatively - you can make a utility function that takes the array, property and target string as arguments and this will allo2w you to search / filter other arrays and by any property and target string .
These two options are shown below and both log out the same results.
const myArray = [
{"name":"Ram", "email":"ram#gmail.com", "userId":"HB000006"},
{"name":"Shyam", "email":"shyam23#gmail.com", "userId":"US000026"},
{"name":"John", "email":"john#gmail.com", "userId":"HB000011"},
{"name":"Bob", "email":"bob32#gmail.com", "userId":"US000106"}
]
// option 1 - direct filtering
const matchingItems = myArray.filter(element => element.userId.includes("US"));
console.log(matchingItems);
// gives - [ { name: 'Shyam', email: 'shyam23#gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32#gmail.com', userId: 'US000106' } ]
//option 2 - create a function that takes arguments and returns the matches
const matches = (arr, prop, str) => {
return arr.filter(element => element[prop].includes(str));
}
console.log(matches(myArray, 'userId', 'US'));
// gives - [ { name: 'Shyam', email: 'shyam23#gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32#gmail.com', userId: 'US000106' } ]

How to transform a nested array into string if it contains null values or just an empty one?

I need to replace nested arrays inside a main array that have null values like lets say [null, null] or the nested arrays that are empty with a string value like "empty".
Saying that we have the following array:
array = [
{
id: 123,
name: 'Peter',
phone: [null, null],
addresses: [{ address1: 'Manchester, UK', address2: 'London, UK' }]
},
{
id: 124,
name: 'Sara',
phone: [],
addresses: [{ address1: 'London, UK', address2: 'Paris, FR' }]
}
];
We see that, the first array has phone: [null, null] and the second has it as []. What I need to do it to transform them into the following:
array = [
{
id: 123,
name: 'Peter',
phone: "empty",
addresses: [{ address1: 'Manchester, UK', address2: 'London, UK' }]
},
{
id: 124,
name: 'Sara',
phone: "empty",
addresses: [{ address1: 'London, UK', address2: 'Paris, FR' }]
}
];
This is an example, and each array might contain multiple nested arrays that have the same [null, null] or [].
I tried the following:
var filtered = this.array.map(subarray => subarray.filter(el => el != null));
from this Stack Overflow answer, but I've got an error saying:
Error: subarray.filter is not a function
Then I tried a second method using lodash's every() and isNull method and property but couldn't figure it out:
let props = [];
props = Array.from(new Set(this.array.flatMap(e => Object.keys(e), [])));
console.log(props)
for (const prop of props) {
this.array.forEach(e => {
if ((Array.isArray(e[prop]) || typeof(e[prop]) === 'object') && e[prop]._.every(_.isNull)) {
console.log(e)
}
});
}
I searched few questions on Stack Overflow but the structure of the arrays are like: [ [1, 2], [1,3]...] and not like my array's structure [{...}, {...}], so I tried some of the solution and got the same error of method 1 above.
Here is a stackblitz.
First loop through the array, and within each object, you can set the phone property:
for(const entry of array) {
const isEmpty = entry.phone.filter(p => p !== null).length === 0;
entry.phone = isEmpty ? 'empty' : entry.phone;
}
Caveat is that this edits your array. One concern about the premise of the question is that you are setting an array property to a string, which is not ideal.
Live Example: https://jsfiddle.net/michaschwab/9ze3p2or/3/, and here's your edited stackblitz: https://stackblitz.com/edit/null-nested-array-into-string-jwhfwn
If you want to not modify your array, this is a way to do it:
const modified = array.map(entry => {
return {...entry, // Copy previous values
phone: entry.phone.filter(p => p !== null).length === 0 ? 'empty' : entry.phone
};
});
Map the array, and use _.mapValues() on each object. For each values that is an array, and full of null values, return 'empty':
const array = [{"id":123,"name":"Peter","phone":[null,null],"addresses":[{"address1":"Manchester, UK","address2":"London, UK"}]},{"id":124,"name":"Sara","phone":[],"addresses":[{"address1":"London, UK","address2":"Paris, FR"}]}];
const result = array.map(o =>
_.mapValues(o, v => // map the values of the object
_.isArray(v) && v.every(_.isNull) ? 'empty' : v // if a value is an array, and all values are null return 'empty'
)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

javascript [Object] inside my object instead of real data

I have the following js code https://repl.it/N0xy/0
I am trying to push some objects into an existing one using some functions:
mylist.push(createMyObject(item.name, item.school, item.teacher))
the result contains :
{ result: true, count: 1, items: [ [ [Object], [Object] ] ] }
instead of :
{ result: true, count: 1, items: [ { name: 'Jacky', school: 'high', teacher: 'good' },
{ name: 'Tom', school: 'college', teacher: 'bad' } ] }
how can i fix this?
thanks
You forgot to do JSON.stringify(obj) in the last statement. Everything else seems fine.
You might want to change the second last line to:
obj.items = create(); as well.
or maybe obj.items = obj.items.concat(create());
You pushed an array to obj.items in stead of the separate items. create() returns an array.
Try this:
create().forEach(function(item) {
obj.items.push(item);
});
OR
let createdItems = create();
for(item of createdItems) {
obj.items.push(item);
}
In your console.log at the end, wrap the obj in a call to JSON.stringify like this:
console.log("print my obj: ",obj);

How to generate new array of objects from existing array of objects using lodash

Can someone help me generate a new array of objects from an existing one using lodash? I've been trying a combination of _.zipObject and map but to no avail... basically, I have an array of objects like:
const names = [
{
first_name: 'nedd',
given_name: 'cersei'
},
{
first_name: 'tyrion',
given_name: 'tywin'
}
]
However, I want it to look like:
[
{
name: 'nedd'
},
{
name: 'cersei'
},
{
name: 'tyrion'
},
{
name: 'tywin'
},
]
I have tried various iterations of:
const newArray = _.zipObject( names, _.fill( Array(names.length), {name: ['first_name' || 'given_name']} ) );
But without any luck... can someone help?
Thanks in advance!
This might work:
_.flatMap(names, (n)=> [{name: n.first_name}, {name: n.given_name}]);
Use _.flatMap combined with _.map:
_.flatMap(names, (nameObj) => _.map(nameObj, (objVal) => { return { name: objVal }; }));

Categories