How best to Compare JSON object values to a fixed array in JScript
I would like to compare JSON values to an array of values but I d'ont know what's the best scenario to go with.
I got a JSON object with expected values (could have 1 value , 2 or more)
I have a DB function that returns a fix number of values, say 10 values all the time and I would like to know if my JSON values matches the right one coming from DB.
Ex:
My JSON var is :
var expValues = {
"id": "123",
"age": 23
};
My DB will push some values to an Array of objects.
Ex:
if ((rs.BOF) && (rs.EOF))
{
//nothing found;
}
else
{
while (!rs.EOF)
{
aDetails.push(
{
"id": rs.fields("id").Value,
"name": rs.fields("name").Value,
"age": rs.fields("age").Value,
"sex": rs.fields("sex").Value,
"hobby": rs.fields("hobby").Value
});
rs.MoveNext();
}
}
rs.close;
//Close connection then return
return aDetails;
basically I want to make sure values coming from JSON match the right ones coming from DB. (id for example).
arrays json loops for-loop jscript
add a comment |
I would like to compare JSON values to an array of values but I d'ont know what's the best scenario to go with.
I got a JSON object with expected values (could have 1 value , 2 or more)
I have a DB function that returns a fix number of values, say 10 values all the time and I would like to know if my JSON values matches the right one coming from DB.
Ex:
My JSON var is :
var expValues = {
"id": "123",
"age": 23
};
My DB will push some values to an Array of objects.
Ex:
if ((rs.BOF) && (rs.EOF))
{
//nothing found;
}
else
{
while (!rs.EOF)
{
aDetails.push(
{
"id": rs.fields("id").Value,
"name": rs.fields("name").Value,
"age": rs.fields("age").Value,
"sex": rs.fields("sex").Value,
"hobby": rs.fields("hobby").Value
});
rs.MoveNext();
}
}
rs.close;
//Close connection then return
return aDetails;
basically I want to make sure values coming from JSON match the right ones coming from DB. (id for example).
arrays json loops for-loop jscript
Pls also put some ref example with full object details ofaDetails
as well
– Nitish Narang
Nov 23 '18 at 16:30
Are you trying to find out if they are completely equal? Or only values that are present in both sides?
– SpeedOfRound
Nov 23 '18 at 16:30
Only expected values that are present in my JSON object. My aDetails array could have up to 50 values :) so kinda I would like to know if my JSON expected values match with the same coming from DB. (id, age always exist in both but need to compare the values)
– fazlook1
Nov 23 '18 at 16:35
1
@fazlook1 I have assumed some values foraDetails
and added an answer. Pls check
– Nitish Narang
Nov 23 '18 at 16:37
add a comment |
I would like to compare JSON values to an array of values but I d'ont know what's the best scenario to go with.
I got a JSON object with expected values (could have 1 value , 2 or more)
I have a DB function that returns a fix number of values, say 10 values all the time and I would like to know if my JSON values matches the right one coming from DB.
Ex:
My JSON var is :
var expValues = {
"id": "123",
"age": 23
};
My DB will push some values to an Array of objects.
Ex:
if ((rs.BOF) && (rs.EOF))
{
//nothing found;
}
else
{
while (!rs.EOF)
{
aDetails.push(
{
"id": rs.fields("id").Value,
"name": rs.fields("name").Value,
"age": rs.fields("age").Value,
"sex": rs.fields("sex").Value,
"hobby": rs.fields("hobby").Value
});
rs.MoveNext();
}
}
rs.close;
//Close connection then return
return aDetails;
basically I want to make sure values coming from JSON match the right ones coming from DB. (id for example).
arrays json loops for-loop jscript
I would like to compare JSON values to an array of values but I d'ont know what's the best scenario to go with.
I got a JSON object with expected values (could have 1 value , 2 or more)
I have a DB function that returns a fix number of values, say 10 values all the time and I would like to know if my JSON values matches the right one coming from DB.
Ex:
My JSON var is :
var expValues = {
"id": "123",
"age": 23
};
My DB will push some values to an Array of objects.
Ex:
if ((rs.BOF) && (rs.EOF))
{
//nothing found;
}
else
{
while (!rs.EOF)
{
aDetails.push(
{
"id": rs.fields("id").Value,
"name": rs.fields("name").Value,
"age": rs.fields("age").Value,
"sex": rs.fields("sex").Value,
"hobby": rs.fields("hobby").Value
});
rs.MoveNext();
}
}
rs.close;
//Close connection then return
return aDetails;
basically I want to make sure values coming from JSON match the right ones coming from DB. (id for example).
arrays json loops for-loop jscript
arrays json loops for-loop jscript
edited Nov 23 '18 at 17:36
fazlook1
asked Nov 23 '18 at 16:27
fazlook1fazlook1
4412
4412
Pls also put some ref example with full object details ofaDetails
as well
– Nitish Narang
Nov 23 '18 at 16:30
Are you trying to find out if they are completely equal? Or only values that are present in both sides?
– SpeedOfRound
Nov 23 '18 at 16:30
Only expected values that are present in my JSON object. My aDetails array could have up to 50 values :) so kinda I would like to know if my JSON expected values match with the same coming from DB. (id, age always exist in both but need to compare the values)
– fazlook1
Nov 23 '18 at 16:35
1
@fazlook1 I have assumed some values foraDetails
and added an answer. Pls check
– Nitish Narang
Nov 23 '18 at 16:37
add a comment |
Pls also put some ref example with full object details ofaDetails
as well
– Nitish Narang
Nov 23 '18 at 16:30
Are you trying to find out if they are completely equal? Or only values that are present in both sides?
– SpeedOfRound
Nov 23 '18 at 16:30
Only expected values that are present in my JSON object. My aDetails array could have up to 50 values :) so kinda I would like to know if my JSON expected values match with the same coming from DB. (id, age always exist in both but need to compare the values)
– fazlook1
Nov 23 '18 at 16:35
1
@fazlook1 I have assumed some values foraDetails
and added an answer. Pls check
– Nitish Narang
Nov 23 '18 at 16:37
Pls also put some ref example with full object details of
aDetails
as well– Nitish Narang
Nov 23 '18 at 16:30
Pls also put some ref example with full object details of
aDetails
as well– Nitish Narang
Nov 23 '18 at 16:30
Are you trying to find out if they are completely equal? Or only values that are present in both sides?
– SpeedOfRound
Nov 23 '18 at 16:30
Are you trying to find out if they are completely equal? Or only values that are present in both sides?
– SpeedOfRound
Nov 23 '18 at 16:30
Only expected values that are present in my JSON object. My aDetails array could have up to 50 values :) so kinda I would like to know if my JSON expected values match with the same coming from DB. (id, age always exist in both but need to compare the values)
– fazlook1
Nov 23 '18 at 16:35
Only expected values that are present in my JSON object. My aDetails array could have up to 50 values :) so kinda I would like to know if my JSON expected values match with the same coming from DB. (id, age always exist in both but need to compare the values)
– fazlook1
Nov 23 '18 at 16:35
1
1
@fazlook1 I have assumed some values for
aDetails
and added an answer. Pls check– Nitish Narang
Nov 23 '18 at 16:37
@fazlook1 I have assumed some values for
aDetails
and added an answer. Pls check– Nitish Narang
Nov 23 '18 at 16:37
add a comment |
2 Answers
2
active
oldest
votes
I have assumed aDetails
to have something like below data.
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
1
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
add a comment |
This is a general purpose way of indexing list of objects for fast retrieval with any configuration of properties.
// javascript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
listOfPropertyToIndex.forEach((p) => {
key.push(""+o[p]);
});
return key.join("_");
};
arrayOfObject.forEach((o) => {
index[objToKey(o)] = o;
});
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
// jscript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
for (var p in o) {
if (o.hasOwnProperty(p)) {
key.push(""+o[p]);
}
}
return key.join("_");
};
for (var i = 0; i < arrayOfObject.length; ++i) {
index[objToKey(arrayOfObject[i])] = o;
}
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
Here is how to use it
var expValues = {
"id": "123",
"age": 23
};
var index = makeIndex(aDetails, ["id","age"]);
var obj = index.match(expValues);
if (obj) {
... obj ...
}
var index_name = makeIndex(aDetails, ["name"]);
var person = {"name":"as2"};
var obj2 = index_name.match(person);
if (obj2) {
... obj2 ...
}
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450122%2fhow-best-to-compare-json-object-values-to-a-fixed-array-in-jscript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have assumed aDetails
to have something like below data.
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
1
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
add a comment |
I have assumed aDetails
to have something like below data.
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
1
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
add a comment |
I have assumed aDetails
to have something like below data.
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
I have assumed aDetails
to have something like below data.
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
let aDetails = [{
"id": "123",
"name": "as",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "1234",
"name": "as1",
"age": 23,
"sex": "m",
"hobby": "abc"
}, {
"id": "12",
"name": "as2",
"age": 23,
"sex": "m",
"hobby": "abc"
}]
var expValues = {
"id": "123",
"age": 23
};
function isObjectMatched(obj) {
return aDetails.some(d => Object.entries(obj).every(([k, v]) => d[k] == v))
}
console.log(isObjectMatched(expValues))
edited Nov 23 '18 at 16:40
answered Nov 23 '18 at 16:37
Nitish NarangNitish Narang
2,9401815
2,9401815
1
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
add a comment |
1
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
1
1
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
This code is amazing and works like a charm :) but I am sad to say I need it in JScript & not JavaScript. Sorry :( I will update my question.
– fazlook1
Nov 23 '18 at 17:35
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
Could this be done in JScript that clean and nice?
– fazlook1
Nov 23 '18 at 19:21
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 I wish I could if I knew syntax of JScript but I don't
– Nitish Narang
Nov 23 '18 at 19:28
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
@fazlook1 As far as I can read online. JScript is more or less same and so you can try this code in JScript. If it works then great else you can evaluate what error is coming and start to resolve that.
– Nitish Narang
Nov 23 '18 at 19:32
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
Thank you :) I appreciate your help. All the syntax does not work in JScript :( I will try to make them JSON string , then split and compare but I might have a challenge where some strings may have comma in them as well.
– fazlook1
Nov 26 '18 at 15:10
add a comment |
This is a general purpose way of indexing list of objects for fast retrieval with any configuration of properties.
// javascript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
listOfPropertyToIndex.forEach((p) => {
key.push(""+o[p]);
});
return key.join("_");
};
arrayOfObject.forEach((o) => {
index[objToKey(o)] = o;
});
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
// jscript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
for (var p in o) {
if (o.hasOwnProperty(p)) {
key.push(""+o[p]);
}
}
return key.join("_");
};
for (var i = 0; i < arrayOfObject.length; ++i) {
index[objToKey(arrayOfObject[i])] = o;
}
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
Here is how to use it
var expValues = {
"id": "123",
"age": 23
};
var index = makeIndex(aDetails, ["id","age"]);
var obj = index.match(expValues);
if (obj) {
... obj ...
}
var index_name = makeIndex(aDetails, ["name"]);
var person = {"name":"as2"};
var obj2 = index_name.match(person);
if (obj2) {
... obj2 ...
}
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
|
show 3 more comments
This is a general purpose way of indexing list of objects for fast retrieval with any configuration of properties.
// javascript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
listOfPropertyToIndex.forEach((p) => {
key.push(""+o[p]);
});
return key.join("_");
};
arrayOfObject.forEach((o) => {
index[objToKey(o)] = o;
});
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
// jscript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
for (var p in o) {
if (o.hasOwnProperty(p)) {
key.push(""+o[p]);
}
}
return key.join("_");
};
for (var i = 0; i < arrayOfObject.length; ++i) {
index[objToKey(arrayOfObject[i])] = o;
}
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
Here is how to use it
var expValues = {
"id": "123",
"age": 23
};
var index = makeIndex(aDetails, ["id","age"]);
var obj = index.match(expValues);
if (obj) {
... obj ...
}
var index_name = makeIndex(aDetails, ["name"]);
var person = {"name":"as2"};
var obj2 = index_name.match(person);
if (obj2) {
... obj2 ...
}
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
|
show 3 more comments
This is a general purpose way of indexing list of objects for fast retrieval with any configuration of properties.
// javascript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
listOfPropertyToIndex.forEach((p) => {
key.push(""+o[p]);
});
return key.join("_");
};
arrayOfObject.forEach((o) => {
index[objToKey(o)] = o;
});
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
// jscript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
for (var p in o) {
if (o.hasOwnProperty(p)) {
key.push(""+o[p]);
}
}
return key.join("_");
};
for (var i = 0; i < arrayOfObject.length; ++i) {
index[objToKey(arrayOfObject[i])] = o;
}
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
Here is how to use it
var expValues = {
"id": "123",
"age": 23
};
var index = makeIndex(aDetails, ["id","age"]);
var obj = index.match(expValues);
if (obj) {
... obj ...
}
var index_name = makeIndex(aDetails, ["name"]);
var person = {"name":"as2"};
var obj2 = index_name.match(person);
if (obj2) {
... obj2 ...
}
This is a general purpose way of indexing list of objects for fast retrieval with any configuration of properties.
// javascript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
listOfPropertyToIndex.forEach((p) => {
key.push(""+o[p]);
});
return key.join("_");
};
arrayOfObject.forEach((o) => {
index[objToKey(o)] = o;
});
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
// jscript version
function makeIndex (arrayOfObject, listOfPropertyToIndex) {
var index = {};
index.objToKey = function (o) {
var key = ;
for (var p in o) {
if (o.hasOwnProperty(p)) {
key.push(""+o[p]);
}
}
return key.join("_");
};
for (var i = 0; i < arrayOfObject.length; ++i) {
index[objToKey(arrayOfObject[i])] = o;
}
index.match = function (object) {
var key = index.objToKey(object);
if (index.hasOwnProperty(key)) {
return index[key];
};
return null;
});
return index;
}
Here is how to use it
var expValues = {
"id": "123",
"age": 23
};
var index = makeIndex(aDetails, ["id","age"]);
var obj = index.match(expValues);
if (obj) {
... obj ...
}
var index_name = makeIndex(aDetails, ["name"]);
var person = {"name":"as2"};
var obj2 = index_name.match(person);
if (obj2) {
... obj2 ...
}
edited Nov 24 '18 at 0:22
answered Nov 23 '18 at 16:56
Dominique FortinDominique Fortin
1,638816
1,638816
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
|
show 3 more comments
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
Will give it a try :) and get back to you
– fazlook1
Nov 23 '18 at 17:38
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
@fazlook1 This is for unique keys. It possible to modify this to return an array of matches when multiple object produce the same index key.
– Dominique Fortin
Nov 23 '18 at 17:44
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
Unfortunately, I changed my question to be in JScript & not in Java Script.
– fazlook1
Nov 23 '18 at 18:07
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
@fazlook1 I've added a jscript version.
– Dominique Fortin
Nov 23 '18 at 20:23
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
Is there an online editor for JScript where I can test those stuff quickly ?
– fazlook1
Nov 26 '18 at 14:46
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450122%2fhow-best-to-compare-json-object-values-to-a-fixed-array-in-jscript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Pls also put some ref example with full object details of
aDetails
as well– Nitish Narang
Nov 23 '18 at 16:30
Are you trying to find out if they are completely equal? Or only values that are present in both sides?
– SpeedOfRound
Nov 23 '18 at 16:30
Only expected values that are present in my JSON object. My aDetails array could have up to 50 values :) so kinda I would like to know if my JSON expected values match with the same coming from DB. (id, age always exist in both but need to compare the values)
– fazlook1
Nov 23 '18 at 16:35
1
@fazlook1 I have assumed some values for
aDetails
and added an answer. Pls check– Nitish Narang
Nov 23 '18 at 16:37