I'm trying to create a simple static web page to mess around with some Lodash functions like _.get(). Their docs only specify how to set the _ variable via Node require() syntax. I don't want Node involved for a simple static page.
Putting lodash.core.min.js in a <script> tag isn't allowing me to test _.get(). Is it possible to do this without Node?
UPDATE (solved)
lodash.core does not contain the get() function. (per github.com/lodash/lodash/wiki/Build-Differences)
Using lodash.min.js lets me do what I want. lodash.core.min.js does not.
Use _.noConflict()
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
<script>
_u = _.noConflict();
users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false },
{ user: 'travis', age: 37, active: true}
];
result = _u.filter(users, function(o) { return o.active; });
console.log(result);
</script>
In https://codepen.io you can simply add lodash as dependency from the quick add. It is a great way to test it:
Also you can use this already prepared pen: test lodash
var users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false },
{ user: 'travis', age: 37, active: true}
];
result = _.filter(users, function(o) { return o.active; });
Related
I'm using React.
Raise the child object to the parent.
I want to delete the unnecessary value and send it.
const before = {
address_check: true,
loan_check: true,
oneroom_disable: true,
info: {
age: 13,
name: gogo,
item: [1,2,3]
},
face: {
life: [1,2,3]
}
};
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
const after = {
address_check: true,
age: 13,
name: gogo,
item: [1,2,3]
};
How can I change the status from before to after?
You can create object from other object like below:-
const after = {
address_check: before.address_check,
age: before.info.age,
name: before.info.name,
item: before.info.item
}
You can do something like this:
// Original data
const before = {
address_check: true,
loan_check: true,
oneroom_disable: true,
info: {
age: 13,
name: gogo,
item: [1,2,3]
},
face: {
life: [1,2,3]
}
};
// Destructure required fields
const {address_check, info: {age, name, item}} = before;
// Put them together for your new object
const after = {
address_check, age, name, item
}
I was looking at the docs for lodash. There is a function called "filter", but it is used in a different way than the "filter" from import filter from "lodash/fp/filter";. For example, this code does not work:
import filter from "lodash/fp/filter";
var users = [
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 40, active: false }
];
const newUsers = filter(users, function (o) {
return !o.active;
});
console.log(newUsers);
However, I can't seem to find the cos for "import filter from "lodash/fp/filter";
Lodash/fp function are iteratee (the callback function) first, data last, and curried, so you can create new functions, and then supply the data:
const { filter } = _; // import { filter } fron 'lodash/fp'
const filterActiveUsers = filter(o => !o.active);
const users = [
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 40, active: false }
];
console.log(filterActiveUsers(users));
// which is identical to
console.log(filter(o => !o.active, users));
<script src='https://cdn.jsdelivr.net/g/lodash#4(lodash.min.js+lodash.fp.min.js)'></script>
i want an array of object with some properties and want to check that object have particular property or not and moreover i also want if that object dosn't have that property in an array then return false if present then return true but i never wanna use if else statement i want to do it with ternary operator but i have some issue in below code inside a function.
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(obj) {
return (users.Alan) ? true : (users.Jeff) ? true : (users.Ryan) ? true : (users.Sarah) ? true : false;
}
console.log(isEveryoneHere(users));
There is a lot to unpack here, but lets start with your user list.
When you store a list it is usually best to use an Array, as this will give you access to the array methods like every.
Moving the "name" into the objects makes it nicely conform to array syntax, and you could use find to get entries by name anyway.
Secondly, now that we have array methods, the ternary approach is redundant. We simply ask if every object in the list has a true online property. Doing it this way greatly increases readability of your code, and saves us from constantly reinventing existing solutions.
Here is an example of the above points:
var users = [{
name: 'Alan',
age: 27,
online: true
}, {
name: 'Jeff',
age: 32,
online: true
}, {
name: 'Sarah',
age: 48,
online: true
}, {
name: 'Ryan',
age: 19,
online: true
}];
function isEveryoneHere(users) {
return users.every(function(user) {
return user.online;
});
}
console.log(isEveryoneHere(users));
I have a collection of entities like this:
var entities = [
{
location: null,
label: 'xyz'
},
{
location: {
city: 'city'
},
label: 'xyz'
}
{
label: 'xyz'
}
];
Trying to filter out entities with location = null, undefined or not existing at all. Using lodash.
When testing the location of the first entity in this way, it's returning the correct answer true:
_.isNil(_(entities[0]).get('location'));
but when I trying to include the isNil in chain, it returns Cannot read property 'valueOf' of null error:
_(entities[0]).get('location').isNil
Can isNil included somehow in the lodash chain?
here is the issue: https://codepen.io/neptune01/pen/qjxJwr
I'll need to use it somehow in this way:
_(entities).omitBy(_.get('location').isNil)
or
_(entities).omitBy(_(entity).get('location').isNil)
isNil() is not chainable.
See a complete list of non-chainable methods here.
You can use it like this, for your particular problem:
_.omitBy(entities, (el) => _.isNil(el.location))
var entities = [{
location: null,
label: 'xyz'
},
{
location: {
city: 'city'
},
label: 'xyz'
}
];
console.log(_.omitBy(entities, (el) => _.isNil(el.location)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
var obj = {
customerInfo: {
fname: "Tom",
lname: "Smith",
age: 23,
height: 160
},
car: {
color: "red",
numWheels: 4
}
};
I would like to modify this object into:
var obj = {
customerInfo: {
fname: "Sally",
lname: "Adams",
mname: "Tully",
age: 23,
height: 160
},
car: {
color: "red",
numWheels: 4
}
};
However, if I do
_.extend(obj, {
customerInfo: {
fname: "Sally",
lname: "Adams",
mname: "Tully"
}
});
the object becomes
{
customerInfo: {
fname: "Sally",
lname: "Adams",
mname: "Tully"
},
car: {
color: "red",
numWheels: 4
}
};
and the age and height have been wiped out.
What do I need to do to preserve data that's nested?
And what about updating more complex objects?
http://jsfiddle.net/j4L18p7k/2/#share
There are better ways of achieving this deep merge behavior you're actually looking for...
You could use Jquery's $.extend API as documented here: http://api.jquery.com/jquery.extend/
I've put together this little example to match your code sample since I believe you that this kind of generic approach to merging is what you're actually looking for, right?
var o = {
customerInfo: {
fname: "Tom",
lname: "Smith",
age: 23,
height: 160,
paymentMethods: {
paypal: {
username: "sally"
}
}
},
car: {
color: "red",
numWheels: 4
}
};
// this wipes out more deeply nested things (paypal disappears)
var merge = $.extend(true, {} , o.customerInfo, {
"fname": "Sally",
lname: "Adams",
mname: "Tully",
paymentMethods: {
visa: {
lastFour: "5555"
}
}
});
console.dir(merge);
http://jsfiddle.net/hrqbyd5c/9/
However pay attention to the JQuery docs
On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not. Deep-extending a cyclical data structure will result in an error.
For needs that fall outside of this behavior, write a custom extend method instead, or use a library like lodash.
Check out lodash's merge API documentation https://lodash.com/docs#merge
Hope this helped some more...
Extend customerInfo directly. Underscore .extend() replaces any key you mention, in this case removing any previous nested keys that aren't in your new object.
_.extend(obj.customerInfo, {
fname: "Sally",
lname: "Adams",
mname: "Tully"
});