Javascript - about Objects - javascript

I would like to know if this is possible:
I want to access a index of an object that point for another index in the same object..
example:
var object = {
edit: function (string) {
alert(string);
},
edit2: "call default edit()"
};
object.edit2("Hello World!!");
How can I do that?
Sorry my english is.. bad

You could just do it like this
var object = {
edit : function(string){
alert(string);
},
edit2 :function(string){
this.edit(string);
}
};
object.edit2("Hello World!!");

How about this:
var object = {edit : function(string){alert(string)},
edit2 : function(string){this.edit(string)}
}
object.edit2("Hello World!!")

I think Javascript allow:
var object = {edit : function(string){alert(string)} };
object.edit2 = object.edit;
object.edit2("Hello World!!")
or scrblnrd3's solution.
This website is international, so i guess you're not the only one who don't speak very well english... (Why are you looking at me ?)

Related

Naming a JSON array using Jquery

I have an array of JSON:
[{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225?v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name":"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples","Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score":44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":"2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44:35Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}]
I want it to be:
{
gitdList: [
{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225? v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},
{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}
]
}
I searched and am finding solutions regarding php only , to use json.encode.
How do I do it using JQUERY?
Have you try like this way? just create an empty object like this object = {} and assign your existing value i.e avatar_urls to it with your desired key i.e gitdList. Hope this will work for you.
var avatar_urls = [{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225?v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name":"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples","Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score":44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":"2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44:35Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}]
var object = {};
object.gitdList = avatar_urls;
console.log(object);
Edit: pretty neat and better way to do it.
var object = { gitdList: avatar_urls};
console.log(object);
maybe like this:
var data=[{"AvatarURL":"https://avatars1.githubusercontent.com/u/7849225?v=4","Name":"simplenlg","Score":22.82041,"Updatedat":"2018-07-21T10:58:33Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/8931462?v=4","Name":"aws-microservices-deploy-options","Score":20.521696,"Updatedat":"2018-07-20T12:22:07Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/4046529?v=4","Name":"useful-jenkins-groovy-init-scripts","Score":21.447626,"Updatedat":"2018-07-18T19:52:02Z"},{"AvatarURL":"https://avatars1.githubusercontent.com/u/6755615?v=4","Name":"dnsjava","Score":34.74705,"Updatedat":"2018-06-28T15:16:45Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/504773?v=4","Name":"luke","Score":19.239859,"Updatedat":"2018-06-28T07:27:26Z"},{"AvatarURL":"https://avatars3.githubusercontent.com/u/536912?v=4","Name":"Wicket-tutorial-examples","Score":37.265644,"Updatedat":"2018-07-14T04:28:50Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/33330803?v=4","Name":"nexus-repository-apt","Score":44.401646,"Updatedat":"2018-07-06T18:28:13Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1321963?v=4","Name":"marc4j","Score":28.282797,"Updatedat":"2018-07-07T15:58:57Z"},{"AvatarURL":"https://avatars0.githubusercontent.com/u/5622390?v=4","Name":"jamonapi","Score":24.564436,"Updatedat":"2018-07-16T07:44:35Z"},{"AvatarURL":"https://avatars2.githubusercontent.com/u/1123352?v=4","Name":"osgi.enroute","Score":7.6444883,"Updatedat":"2018-07-17T08:26:51Z"}];
var new_data={ 'gitdList': data };
console.log(new_data);

How can I do more elegantly several _.has checks?

I have an object like this
myObject:{"property1": "valueX",
"property2": "valueY",
"property3": "valueZ",
"property4": "valueV",
"property5": "valueW"}
and I want to make sure that none of my object properties names match several strings.
The most intuitive way I found is this one:
if( !_.has(myObject, "TestingValue1")&&
!_.has(myObject, "TestingValue2")&&
!_.has(myObject, "TestingValue3")&&
!_.has(myObject, "TestingValue4")){
//do something
}
But if I have too much property names to check, it is becoming quite a large piece of code.
I am trying to come up with a more elegant solution. I feel it is almost ok but I does not appear to work (it always returns true). Here it is:
var TestingValues = ["TestingValue1", "TestingValue2", "TestingValue3"]
if (!_.every(TestingValues, _.partial(_.has, myObject))){
//do something
}
Can you tell me what is wrong? How should I declare TestingValues?
EDIT:
#Sergiu Paraschiv I used different values in myObject and the test array only for making it easier to read. Of course I tested it with identical values.
You are right, I just realized it works. I didn't at first because it does not work as intended. I mixed things up: I want to return false if any of the item in the string array matches any of the attributes of myObject
You can do :
var TestingValues = [ "TestingValue1", "TestingValue2", "TestingValue3" ];
if(!_.isEmpty(_(myObject).pick(TestingValues)){...
or as you suggested yourself :
if (!_.some(TestingValues, _.partial(_.has, myObject)))
Alternative:
_some(TestValues, function(test) { return _.indexOf(_.values(myObject), test) != -1});
You can try
var testingValues = ["TestingValue1", "TestingValue2", "TestingValue3"];
var myObj = {
"property1": "valueX",
"property2": "valueY",
"property3": "valueZ",
"property4": "valueV",
"property5": "valuez"
};
var result = _.every(myObj, function(value, key, obj){
return !_.contains(testingValues, value);
});
console.log(result);

How to return only value of a field in mongodb

After applying the find operation in mongodb.. i get the following list of documents..
db.users.find(....)
i got:
{ "text" : "Hey" }
{ "text" : "Hi" }
{ "text" : "Hello" }
{ "text" : "yes" }
How can i convert it into
["Hey","Hi","Hello","yes"].
i tried
db.users.find(...).map( function(u) { return "u.text"; } )
but it is giving error!
Not sure what you language implementation is but the basic concept is:
var result = []
db.users.find().forEach(function(u) { result.push(u.text) })
And the returned value to result is:
["Hey","Hi","Hello","yes"]
At first db.users.find(...).map() didn't work because db.users.find(...) doesn't return you a real array.
So you need to convert to array at first.
db.users.find(...).toArray()
Then if you apply map() function will work
db.users.find(...).toArray().map( function(u) { return u.text ; } )
Another simple trick is using .forEach()
This will do the trick
var cursor = db.users.find(...); // returns cursor object which is a pointer to result set
var results = [];
cursor.forEach(
function(row) {
results.push(row.text);
});
results //results will contain the values
Another option is simply to use distinct:
db.users.distinct("first_name");
Would return:
[
"John",
"Jennifer",
...
]
you can use
var u=db.users.find({...},{text:1,_id:0})
while(u.hasNext()){print(u.Next().text);}
The correct answer here is the method .distinct() (docs)
In your case try it like this:
db.users.find(....).distinct('text')
That will return only the values.
best way is :
db.users.distinct("text");
["Hey","Hi","Hello","yes"].
You will get further information regargding this topic here : mongodb distinct

json does not include object name during stingify

I am trying to convert a javascript object into json using JSON.stringify() method. My problem is that when it stingify the object , it only stingify the object's key and values.It does not include the object name.
I want the output like {"Color" : "{"Name":"background","Type":"Color","Value":"Red"}"} but the output comes is {"Name":"background","Type":"Color","Value":"Red"}.
Here is Demo .
I know that json works like this but i want the way to acheive this. Thanks in advance !
The object doesn't know its own name, so you need to explicitly name it in the JSON:
JSON.stringify({'Color': color});
You can make your createColor function return an object with a property that contains the color object:
function createColor() {
var color = new Color();
color.Name = "background";
color.Type = "Color";
color.Value = "Red";
return { Color: color };
}
Demo: http://jsfiddle.net/Guffa/hMwjq/5/
try this :
(function () {
var color = createColor();
jsonObject = {};
jsonObject.color = color;
var json = JSON.stringify(jsonObject);
console.log(json);
})();
see full Demo

Add additional object to variable in javascript?

I have a variable that may contain objects or may be undefined. I wish to add additional objects to this variable. How do I do that?
code example when applicable:
function(){
var comments;
if(fbposts[fbpost].comments.count){
for(var comment in fbposts[fbpost].comments.data){
comments = ({
name: fbposts[fbpost].comments.data[comment].from.name,
link: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id,
img: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id+"/picture",
message: fbposts[fbpost].comments.data[comment].message,
created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time)),
})
}
}
return comments;}(),
Test if it is undefined and if so assign it to an empty object:
if (typeof yourVar === "undefined")
yourVar = {};
yourVar.additionalObject1 = { something : "test" };
yourVar.additionalObject2 = { something : "else" };
EDIT: OK, now that you've added code to your question, it seems like your comments variable should be an array, since you are adding to it in a loop. So I think you'd want to do something like this:
(function(){
var comments = [];
if(fbposts[fbpost].comments.count){
for(var comment in fbposts[fbpost].comments.data){
comments.push({
name: fbposts[fbpost].comments.data[comment].from.name,
link: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id,
img: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id+"/picture",
message: fbposts[fbpost].comments.data[comment].message,
created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time)),
});
}
}
return comments;
})();
comments will thus contain one element for each comment in your source data. If there are no comments it will be an empty array. (If you want it to return undefined if there are no comments then leave the variable declaration where it is as var comments and add comments=[]; just inside the if statement.)
If you're using jQuery (and why wouldn't you be?), then you want to look at $.extend()
http://api.jquery.com/jQuery.extend/

Categories

Resources