How to bind two object to one in JS - javascript

I have a query filter in my code, I want to pass these two queries as one like this:
case1
con = {'eq':['case1':dosome]}
case2
con = {'eq':['case1':dosome]}
I need to bind these two at the end like this
{'eq':['case1':dosome],'eq':['case2':dosome]}
the key eq should remain same.

Why not use an object with two properties like
{ eq: { case1: dosome, case2: dosome } }

Sounds like you want an array of objects:
var your_array = new Array();
var con1 = {'eq':{'case1':dosome}}
your_array.push(con1)
var con2 = {'eq':{'case2':dosome}}
your_array.push(con2)
You can then use the methods for accessing parts of the array -> http://www.w3schools.com/js/js_array_methods.asp

Use merge() function. Also this will give error: ['case1':dosome]. Try this
<script>
var obj1 = {'eq':{'case1':dosome}};
var obj2 = {'eq':{'case2':dosome}}
var merge = obj1.merge(obj2);
</script>
Hope this helps

You can try this:
{ eq: [
{ condition: 'case1', action: doSome } ,
{ condition: 'case2', action : doSomeOther }
]}

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 make this javascript code cleaner? Reuse variables

I am fairly new to Javascript.I have to count some values and my current vairable looks like this :
var mainObj : {
"main1":{
"var1":0,
"var2":0,
"var3":0
},
"main2":{
"var1":0,
"var2":0,
"var3":0
},
"main3":{
"var1":0,
"var2":0,
"var3":0
}
}
Is there a way i can reuse a single object for all that?
Something like :
var mainObj :{
"main1":{
someOBjInstance
},
,
"main2":{
someOBjInstance
} "main3":{
someOBjInstance
}
}
It is just a matter of creating copies of the object:
var subObject = {
"var1":0,
"var2":0,
"var3":0
};
var mainObj = {
"main1": subObject,
"main2": Object.create(subObject),
"main3": Object.create(subObject)
};
If the values are just a bunch of numbers which you want to increment, then you could use arrays instead of Object, which will be faster and easier to manipulate:
var mainArray = [[0,0,0],[0,0,0],[0,0,0]]

Store object inside object and update

I am trying to store an array inside and object like this:
var fieldData = {
checkedItem: {
fieldID : “1234”,
SelectedFields : []
}
checkedItem: {
fieldID : “12345”,
SelectedFields : []
}
}
I then want to also replace all selected fields to this object at a later stage.
I am a newbie to this so not sure how it would be done, I have tried everything I can think of!
The later changes to the object will referenced by fieldID.
I have tried stuff like:
fieldData["fieldID"] = selectedFieldSeq;
fieldData[selectedFieldSeq]["SelectedFields"] = $('#Tree').jqxTree('getCheckedItems');
$('#Tree').jqxTree('getCheckedItems');
returns an array of checked items on my tree.
This should do it:
'fieldID = $('#Tree').jqxTree('getCheckedItems');'
'fieldData.SelectedFields = fieldID'
There is a problem with this line :
fieldData[selectedFieldSeq]["SelectedFields"]
fieldData[selectedFieldSeq] is not defined, so it's return undefined
You need to initialize it before using it :
if (!fieldData[selectedFieldSeq]) {
fieldData[selectedFieldSeq] = {
SelectedFields : []
};
}
After, you can assign some value to SelectedFields.
Or did you want to simply do this : fieldData.SelectedFields = ...; ?

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

self-reference within declaration of a structured javascript variable

Suppose I declare a structured Javascript variable like this:
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
,'summary':this.list[1]['lname']
}
Question: How can I ensure that
this.list[1]['lname']
equals 'wilde' and that my "this" is actually referring
to what I intend?
You could also make this work in a function.
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
,'summary':function(){return this.list[1]['lname']}
}
alert(mydata.summary());
this won't work as written, but you could do:
var mydata = {'list':[
{'fname':'george','lname':'washington'}
,{'fname':'oscar','lname':'wilde'}
,{'fname':'james','lname':'polk'}
]
}
mydata.summary = mydata.list[1]['lname'];

Categories

Resources