I have an object like below. Sometimes it will contain status sometimes not. I want to convert status value format into Number when it in object.
var obj={
name: 'abc',
status: '1',
updated_time: {
'$gt': 2019-11-03T00:00:00.000Z,
'$lt': 2019-11-03T15:23:55.838Z
}
}
i've tried as below but it's not converted;
if(obj.status){
parseInt(obj.status)
}
console.log("$$$$$$$$$$$$$$$$",obj)
console.log print
$$$$$$$$$$$$$$$${
name: 'abc',
status: '1',
updated_time:
{ '$gt': 2019-11-03T00:00:00.000Z,
'$lt': 2019-11-03T15:23:55.838Z }
}
You need to assign back parsed value to object.status. parseInt method just evaluate the value it doesn't changes value by itself in object. you need to change value manually on object.
var obj = {
name: 'abc',
status: '1',
updated_time: {
'$gt': '2019-11-03T00:00:00.000Z',
'$lt': '2019-11-03T15:23:55.838Z'
}
}
if (obj.status) {
obj.status = parseInt(obj.status)
}
console.log("$$$$$$$$$$$$$$$$", obj)
This is just a simple mistake mate. You haven't assigned the value.
You can do that by :
if(obj.status){
obj.status = parseInt(obj.status); //obj.status is being reassigned
}
Hope it helps :)
parseInt returns a value and you need to assign this value to object.status property:
if(obj.status){
obj.status = parseInt(obj.status)
}
Related
I have an array which contains following objects.
myArray = [
{ item: { id: 111557 } },
{ item2: { id: 500600 } }]
and I have a variable
targetItemID = '111557'
Note that one is string, and the ones in array are numbers. I'm trying to get the object having the correct item id.
Here is what I have tried,
myArray = [
{ item: { id: 111557 } },
{ item2: { id: 500600 } }]
targetItemID = '111557'
var newArray = myArray.filter(x => {
console.log(x.item.id.toString())
console.log(targetItemID.toString())
x.item.id.toString() === itemID.toString()
})
console.log(newArray);
I expect all matching objects to be added to 'newArray'. I tried to check the values before comparison, They are both strings, they seem exactly same, but my newArray is still empty.
Your second object doesn't have an item property and should.
You need a return in your filter function.
You must compare x.item.id against targetItemID, not itemID. Since you are using console.log() you would have seen and error of itemID id not defined ;).
myArray = [
{ item: { id: 111557 } },
{ item: { id: 500600 } }
];
targetItemID = '111557'
var newArray = myArray.filter(x => {
//console.log(x.item.id.toString())
//console.log(targetItemID.toString())
return x.item.id.toString() === targetItemID.toString();
});
console.log(newArray);
There are a few issues here. First, not all your objects have an item property, so you'll need to check it exists. Second, you're comparing them against a non-existent itemID instead of targetItemID, and finally, and #bryan60 mentioned, if you open a block in an anonymous lambda, you need an explicit return statement, although, to be honest, you really don't need the block in this case:
var newArray =
myArray.filter(x => x.item && x.item.id && x.item.id.toString() === targetItemID)
you need to return for filter to work:
return x.item.id.toString() === itemID.toString();
Hi Im still learning node and trying something cool with javascript nodejs.
Meanwhile I got stuck when pass separate "where" sequelize statement into one.
Okay, this is my current code :
var periodsParam = {};
periodsParam = {
delete: 'F',
tipe: 1,
variantid: (!ctx.params.id ? ctx.params.id : variants.id)
};
if (ctx.query.country) {
periodsParam = {
country: ctx.query.country
};
}
console.log(periodsParam);
From code above, its always return { country: 'SG' } , but I want to return { delete: 'F', tipe: 1, variantid: 1, country: 'SG' }
How can I resolve that ?
Anyhelp will appreciate, thankyouu.
The problem is, you're using = sign with periodsParam 3 times and you end up with periodsParam returning only country, because of this lines:
if (ctx.query.country) {
periodsParam = {
country: ctx.query.country
};
}
Instead of assigning new object to periodsParam, use dot notation to add another key-value pair, like this:
if (ctx.query && ctx.query.country) { //before accesing .country check if ctx.query is truthy
periodsParam.country = ctx.query.country;
}
As #Paul suggested, condition should be ctx.query && ctx.query.country - it will prevent TypeError if ctx.query is undefined.
The problem was that you were always re initializing it. You should set it as a property of the existing object.
Update from
periodsParam = {
country: ctx.query.country
};
to
periodsParam.country = ctx.query.country;
You can also just assign the Object like this:
periodsParam = Object.assign({}, periodsParam, { country: ctx.query.country });
I have a parsed JSON Object like this.
[ { id: 973276, raw: 'Apple Tree Childcare', extractedData: null },
{ id: 973576, raw: 'Yes', extractedData: 'Yes' },
{ id: 973567, raw: 'Road', extractedData: null } ]
Now on this, I am looking to get value of raw or extractedData, when I know value of id.
I tried find() and filter() functions, but couldn't get it. Here's my code.
function getSchoolName(school) {
return school.id === '973276';
}
var replies = JSON.parse(event.replyHistory);
console.log(replies); // output is above
console.log(replies.filter(getSchoolName));//[]
console.log(replies.find(getSchoolName)); //undefined
Can someone guide me the right way to do it? TIA
return school.id === '973276';
'973276' is string
but your object id is number
you can change code to
return school.id == '973276';
The use of JavaScript to achieve the depth of two objects is comapre, if true is not equal to equal returns, returns false. The first parameter compare to the original object, second parameters for the comparison of the target object, the object attribute exists in value only exists, the property does not exist in the original object if the object, it will return directly false. Here are some examples.
const object = {
id: 1,
name: 'test',
product: {
id: 1,
name: 'product'
},
updatedAt: 'now'
};
const objectA = {
name: 'test',
product: {
name: 'product'
}
};
const objectB = {
name: 'test',
product: {
name: 'anotherProduct'
}
};
compare(object, objectA) // return true
compare(object, objectB) // return false
Here is the solution i made,
fiddle - https://jsfiddle.net/jayjoshi64/qn70yosx/5/
it has recursive function which will check if two given object is object or one of implicit object.
if it is object, it will again check recursively if they are same.
code of the function compare..
it is working for your objects.!
function compare(first,second)
{
//alert("once");
var flag=true;
if(typeof second=='object')
{
//if the variable is of type object
if(typeof first!='object')
{
flag=false
}
else
{
//check every key of object
$.each(second, function(key, value) {
//if socond's key is available in first.
if(first.hasOwnProperty(key))
{
//recursive call for the value.
if(!compare(first[key],second[key]))
{
flag=false;
}
}
else
{
flag=false
}
});
}
}
else
{
// if two objects are int,string,float,bool.
if(first!=second)
{
flag=false;
}
}
return(flag);
}
I have the following object names $scope.parameters. When i execute console.log as shown below, i get this result
console.log($scope.parameters);
Result
Object { Name: "Diana", id: 234, Size: Object, Location: Object, Details: "none" }
Name: "Diana"
id: 234,
Size: Object
Location: Object
Details: "none"
As the result shows the elements Size and Location as Object, i want it to be replaced with null. Also, i want it to be dynamic. for e.g. if any of the above elements are Object, it should replace it to null automatically.
Can someone please let me know how to achieve this.
Test out each key of the object with its type if object make them null
if (Object.getPrototypeOf($scope.parameters.Size) === Object.prototype) {
// True if its object change it to null
$scope.parameters.Size = null;
} else {
// do nothing
}
Make a function which takes parameters to test it out and return.
angular.forEach($scope.parameters, function(value, key) {
if(typeof value === "object"){
console.log(key);
$scope.parameters[key] = null;
}
});