我在 Java有物体:
var obj = {
"a": "test1",
"b": "test2"
}
我如何检查该物体中的试验1作为价值存在?
我在 Java有物体:
var obj = {
"a": "test1",
"b": "test2"
}
我如何检查该物体中的试验1作为价值存在?
你们可以把目标的价值变成一个阵列,并检验存在扼杀。 缔约国认为,该目标没有被推崇,而扼杀是确切的:
var obj = { a: test1 , b: test2 };
if (Object.values(obj).indexOf( test1 ) > -1) {
console.log( has test1 );
}
https://developer.mozilla.org/en-US/docs/Web/Java/Reference/Global_Objects/Object/ Values
Shortest ES6+ one liner:
let exists = Object.values(obj).includes("test1");
您可使用“Array”方法。
var exists = Object.keys(obj).some(function(k) {
return obj[k] === "test1";
});
Try:
var obj = {
"a": "test1",
"b": "test2"
};
Object.keys(obj).forEach(function(key) {
if (obj[key] == test1 ) {
alert( exists );
}
});
或
var obj = {
"a": "test1",
"b": "test2"
};
var found = Object.keys(obj).filter(function(key) {
return obj[key] === test1 ;
});
if (found.length) {
alert( exists );
}
这些数值不使用<代码>NaN和-0
。 您可使用(而不是=
)ECMA第6版的新内容:
Object.is(obj[key], value);
With modern browsers you can also use:
var obj = {
"a": "test1",
"b": "test2"
};
if (Object.values(obj).includes( test1 )) {
alert( exists );
}
Use a
for (let k in obj) {
if (obj[k] === "test1") {
return true;
}
}
您可使用以下网址:https://develloper.mozilla.org/en-US/docs/ Web/Java /Reith/Global_omess/Oget/ Values”rel=“no followlow noreferer”>。
The
Object.values()
method returns an array of a given object s own enumerable property values, in the same order as that provided by afor...in
loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
http://developer.mozilla.org/en-US/docs/Web/Java/Reference/Global_Objects/Array/indexOf”rel=“nofollow noreferer” 方法:
The
indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.
例如:
Object.values(obj).indexOf("test") >= 0
更确切的例子如下:
var obj = {
"a": "test1",
"b": "test2"
}
console.log(Object.values(obj).indexOf("test1")); // 0
console.log(Object.values(obj).indexOf("test2")); // 1
console.log(Object.values(obj).indexOf("test1") >= 0); // true
console.log(Object.values(obj).indexOf("test2") >= 0); // true
console.log(Object.values(obj).indexOf("test10")); // -1
console.log(Object.values(obj).indexOf("test10") >= 0); // false
对于一名一班子,我要说:
exist = Object.values(obj).includes("test1");
console.log(exist);
我对所有这些例子进行了测试,我在Node.js v8.11.2上进行了试验。 作为选择最佳选择的指南。
let i, tt;
const obj = { a: test1 , b: test2 , c: test3 , d: test4 , e: test5 , f: test6 };
console.time("test1")
i = 0;
for( ; i<1000000; i=i+1) {
if (Object.values(obj).indexOf( test4 ) > -1) {
tt = true;
}
}
console.timeEnd("test1")
console.time("test1.1")
i = 0;
for( ; i<1000000 ; i=i+1) {
if (~Object.values(obj).indexOf( test4 )) {
tt = true;
}
}
console.timeEnd("test1.1")
console.time("test2")
i = 0;
for( ; i<1000000; i=i+1) {
if (Object.values(obj).includes( test4 )) {
tt = true;
}
}
console.timeEnd("test2")
console.time("test3")
i = 0;
for( ; i<1000000 ; i=i+1) {
for(const item in obj) {
if(obj[item] == test4 ) {
tt = true;
break;
}
}
}
console.timeEnd("test3")
console.time("test3.1")
i = 0;
for( ; i<1000000; i=i+1) {
for(const [item, value] in obj) {
if(value == test4 ) {
tt = true;
break;
}
}
}
console.timeEnd("test3.1")
console.time("test4")
i = 0;
for( ; i<1000000; i=i+1) {
tt = Object.values(obj).some((val, val2) => {
return val == "test4"
});
}
console.timeEnd("test4")
console.time("test5")
i = 0;
for( ; i<1000000; i=i+1) {
const arr = Object.keys(obj);
const len = arr.length;
let i2 = 0;
for( ; i2<len ; i2=i2+1) {
if(obj[arr[i2]] == "test4") {
tt = true;
break;
}
}
}
console.timeEnd("test5")
Output on my server
test1: 272.325 ms
test1.1: 246.316 ms
test2: 251.98 0ms
test3: 73.284 ms
test3.1: 102.029 ms
test4: 339.299 ms
test5: 85.527 ms
你可以尝试
var obj = {
"a": "test1",
"b": "test2"
};
const findSpecificStr = (obj, str) => {
return Object.values(obj).includes(str);
}
findSpecificStr(obj, test1 );
你可以尝试:
function checkIfExistingValue(obj, key, value) {
return obj.hasOwnProperty(key) && obj[key] === value;
}
var test = [{name : "jack", sex: F}, {name: "joe", sex: M}]
console.log(test.some(function(person) { return checkIfExistingValue(person, "name", "jack"); }));
在新版本中,如果script现在能够用<条码>检查吗?操作。
它对物体或nes物的简单易懂的奥约检查值
var obj = {
"a": "test1",
"b": "test2"
}
if(obj?.a) return "i got the value"
Similarly since in Javascript most used primitive type is Object
我们也可以利用这些阵列、职能等。
aFunc = () => { return "gotcha"; }
aFunc?.() // returns gotcha
myArray = [1,2,3]
myArray?.[3] // returns undefined
Thanks
Best way to find value exists in an Object using Object.keys()
obj = {
"India" : {
"Karnataka" : ["Bangalore", "Mysore"],
"Maharashtra" : ["Mumbai", "Pune"]
},
"USA" : {
"Texas" : ["Dallas", "Houston"],
"IL" : ["Chicago", "Aurora", "Pune"]
}
}
function nameCity(e){
var finalAns = []
var ans = [];
ans = Object.keys(e).forEach((a)=>{
for(var c in e[a]){
e[a][c].forEach(v=>{
if(v === "Pune"){
finalAns.push(c)
}
})
}
})
console.log(finalAns)
}
nameCity(obj)
ES2017 introduces a new method called Object.values() that allows you to return an array of own enumerable property’s values of an object.
如果在上存在价值,你可以这样做。 反对:
let found = Object.values(africanCountries).includes( Nigeria );
if (found) {
// code
}
如果在上存在,也可以检查。 目标要点:
let found = Object.keys(africanCountries).includes( Nigeria );
if (found) {
// code
}
getValue = function (object, key) {
return key.split(".").reduce(function (obj, val) {
return (typeof obj == "undefined" || obj === null || obj === "") ? obj : (_.isString(obj[val]) ? obj[val].trim() : obj[val]);}, object);
};
var obj = {
"a": "test1",
"b": "test2"
};
Function called:
getValue(obj, "a");
var obj = {"a": "test1", "b": "test2"};
var getValuesOfObject = Object.values(obj)
for(index = 0; index < getValuesOfObject.length; index++){
return Boolean(getValuesOfObject[index] === "test1")
}
The Object.values() method returned an array (assigned to getValuesOfObject) containing the given object s (obj) own enumerable property values. The array was iterated using the for
loop to retrieve each value (values in the getValuesfromObject) and returns a Boolean() function to find out if the expression ("text1" is a value in the looping array) is true.
For complex structures, here is how I do it:
const obj = {
test: [{t: t }, {t1: t1 }, {t2: t2 }],
rest: [{r: r }, {r1: r1 }, {r2: r2 }]
}
console.log(JSON.stringify(obj).includes(JSON.stringify({r1: r1 }))) // returns true
console.log(JSON.stringify(obj).includes(JSON.stringify({r1: r2 }))) // returns false
To check is value exists in each object key in javascript and want to return the object.
let arr = [{
firstname: "test1",
middlename: "test2",
surname: "test3",
age:20
},
{
firstname: "test4",
middlename: "test5",
surname: "test6",
age:25
},
{
firstname: "test7",
middlename: "test2",
surname: "test8",
age:30
}];
let filteredObjects = arr.filter((item) => Object.keys(item).some(key => item[key] === "test2"));
这将通过比较每个物体的每一关键配对物来回物体。
答复:
[{
firstname: "test1",
middlename: "test2",
surname: "test3",
age:20
},
{
firstname: "test7",
middlename: "test2",
surname: "test8",
age:30
}]
希望是有助益的!
www.un.org/Depts/DGACM/index_french.htm
const car = { make: Honda , model: Accord , year: 1998 };
console.log( make in car);
// Expected output: true
delete car.make;
if ( make in car === false) {
car.make = Suzuki ;
}
console.log(car.make);
// Expected output: "Suzuki"
Source https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
<代码>_.has(>方法用于检查该途径是否为物体的直接财产。 如果存在这条道路,它就会恢复真实。
var object = { a : { b : 2 } }; console.log(_.has(object, a.b )); console.log(_.has(object, [ a , b ])); console.log(_.has(object, [ a , b , c ]));
Output: true true false
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.