Javascript 使用JS/jQuery获取数组中对象的位置
假设我有一个数组,如下所示:Javascript 使用JS/jQuery获取数组中对象的位置,javascript,jquery,Javascript,Jquery,假设我有一个数组,如下所示: [ { "name": "list", "text": "SomeText1" }, { "name": "complex", "text": "SomeText2", "config": { "name": "configItem", "text": "SomeText3", "anothe
[
{
"name": "list",
"text": "SomeText1"
},
{
"name": "complex",
"text": "SomeText2",
"config": {
"name": "configItem",
"text": "SomeText3",
"anotherObject": {
"name": "anotherObject1",
"text": "SomeText4"
}
}
}
]
我使用这段很棒的代码来获取具有特定键()的所有对象。在我的示例中,它是getObjects(data,'text','')
,它将返回所有节点作为对象,这是由于文本作为键出现的缘故
我唯一的问题是,我需要知道返回对象在整个数组中的位置
有什么办法得到它吗?或者至少是与阵列相关的对象深度
getObjects(r,'text','')[0]
(name=list)->深度1
getObjects(r,'text','')[1]
(name=complex)->深度1
getObjects(r,'text','')[2]
(name=configItem)->depth 2通过以下方法更改getObjects函数:
function getObjects(obj, key, val, depth) {
var objects = [];
depth = typeof depth !== 'undefined' ? depth : 0;
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
depth ++;
objects = objects.concat(getObjects(obj[i], key, val, depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push({"obj":obj,"depth": depth});
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push({"obj":obj,"depth": depth});
}
}
}
return objects;
}
现在您可以获得深度和对象。通过以下方法更改getObjects函数:
function getObjects(obj, key, val, depth) {
var objects = [];
depth = typeof depth !== 'undefined' ? depth : 0;
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
depth ++;
objects = objects.concat(getObjects(obj[i], key, val, depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push({"obj":obj,"depth": depth});
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push({"obj":obj,"depth": depth});
}
}
}
return objects;
}
现在您可以获得深度和对象。通过以下方法更改getObjects函数:
function getObjects(obj, key, val, depth) {
var objects = [];
depth = typeof depth !== 'undefined' ? depth : 0;
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
depth ++;
objects = objects.concat(getObjects(obj[i], key, val, depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push({"obj":obj,"depth": depth});
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push({"obj":obj,"depth": depth});
}
}
}
return objects;
}
现在您可以获得深度和对象。通过以下方法更改getObjects函数:
function getObjects(obj, key, val, depth) {
var objects = [];
depth = typeof depth !== 'undefined' ? depth : 0;
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
depth ++;
objects = objects.concat(getObjects(obj[i], key, val, depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push({"obj":obj,"depth": depth});
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push({"obj":obj,"depth": depth});
}
}
}
return objects;
}
现在,您将获得深度和对象。您将需要沿着这些线进行操作:
function getObjectsDepth(obj, key, val, depth) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjectsDepth(obj[i], key, val,++depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(depth);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(depth);
}
}
}
return objects;
}
这将返回对象的深度,而不是对象本身,只需根据您想要的计数方式传递0或1作为最后一个参数。
如果您同时想要对象和深度,您需要
obj.push({'obj':obj,'depth':depth})
您需要沿着以下几行进行操作:
function getObjectsDepth(obj, key, val, depth) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjectsDepth(obj[i], key, val,++depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(depth);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(depth);
}
}
}
return objects;
}
这将返回对象的深度,而不是对象本身,只需根据您想要的计数方式传递0或1作为最后一个参数。
如果您同时想要对象和深度,您需要
obj.push({'obj':obj,'depth':depth})
您需要沿着以下几行进行操作:
function getObjectsDepth(obj, key, val, depth) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjectsDepth(obj[i], key, val,++depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(depth);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(depth);
}
}
}
return objects;
}
这将返回对象的深度,而不是对象本身,只需根据您想要的计数方式传递0或1作为最后一个参数。
如果您同时想要对象和深度,您需要
obj.push({'obj':obj,'depth':depth})
您需要沿着以下几行进行操作:
function getObjectsDepth(obj, key, val, depth) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjectsDepth(obj[i], key, val,++depth));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(depth);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(depth);
}
}
}
return objects;
}
这将返回对象的深度,而不是对象本身,只需根据您想要的计数方式传递0或1作为最后一个参数。
如果您同时想要对象和深度,您需要
obj.push({'obj':obj,'depth':depth})
井的可能副本,我不需要对象的最深层次-我需要数组中给定对象的确切深度。不,这个链接对我没有帮助。可能的重复可能的重复好吧,我不需要物体的最深层次-我需要数组中给定物体的确切深度。不,这个链接对我没有帮助。可能的重复可能的重复好吧,我不需要物体的最深层次-我需要数组中给定物体的确切深度。不,这个链接对我没有帮助。可能的重复可能的重复好吧,我不需要物体的最深层次-我需要数组中给定物体的确切深度。所以不,这个链接帮不了我。可能是重复的谢谢!我还有一个问题。如果我调用getObjects(r,'config',''),我的返回值为null,尽管数组中有一个对象。这是因为'config'是一个对象,所以它只是被跳过。你应该添加objects.push(obj)代码>在if之后(对象的类型[i]=“对象”){
甚至可以。这会给我很多我在查询Key=text时不想要的返回值,我想我需要对此进行调试-提前感谢。实际上有一个更优雅的解决方案,只需删除单词else
谢谢!我还有一个问题。如果我调用getObjects(r,'config','')虽然我的数组中有一个对象,但我的返回值为null。这是因为'config'是一个对象,所以跳过它。你应该添加objects.push(obj);
在if(typeof obj[i]='object')之后{
甚至可以。这会给我很多我在查询Key=text时不想要的返回值,我想我需要对此进行调试-提前感谢。实际上有一个更优雅的解决方案,只需删除单词else
谢谢!我还有一个问题。如果我调用getObjects(r,'config','')虽然我的数组中有一个对象,但我的返回值为null。这是因为'config'是一个对象,所以跳过它。你应该添加objects.push(obj);
在if(typeof obj[i]='object')之后{
甚至可以。这会给我很多我在查询Key=text时不想要的返回值,我想我需要对此进行调试-提前感谢。实际上有一个更优雅的解决方案,只需删除单词else
谢谢!我还有一个问题。如果我调用getObjects(r,'config','')虽然我的数组中有一个对象,但我的返回值为null。这是因为'config'是一个对象,所以跳过它。你应该添加objects.push(obj);
在if(typeof obj[i]='object')之后{
来实现这一点。这会给我很多我在查询Key=text时不想要的返回值。我想我需要对此进行调试-提前感谢。实际上有一个更优雅的解决方案,只需删除单词else