var caseOne = [
{"name":"Felicity", "gender":"F", ... ... ,"type":"Admin"},
{"name":"Tony", "gender":"M", ... ... ,"type":""},
.
.
.
.
{"name":"Super Man", "gender":"M", ... ... ,"type":""},
{"name":"Wonder Women", "gender":"F", ... ... ,"type":""},
{"name":"Hulk", "gender":"M", ... ... ,"type":"User"},
{"name":"Loky", "gender":"M", ... ... ,"type":"User"},
{"name":"Thore", "gender":"M", ... ... ,"type":"Admin"}
];
var caseTwo = [
{"name":"Felicity", "gender":"F", ... ... ,"type":"Admin"},
{"name":"Tony", "gender":"M", ... ... ,"type":""},
.
.
.
.
{"name":"Super Man", "gender":"M", ... ... ,"type":""},
{"name":"Wonder Women", "gender":"F", ... ... ,"type":""},
{"name":"Hulk", "gender":"M", ... ... ,"type":"User"},
{"name":"Loky", "gender":"M", ... ... ,"type":"User"},
{"name":"Thore", "gender":"M", ... ... ,"type":"Admin"},
{"name":"Bat man", "gender":"M", ... ... ,"type":""}
];
我有这种阵列。我想要在lastIndexOf "type":""
之后的所有记录,意味着从cansOne
它将返回最后3条记录,并且从caseTwo
它将返回0条记录。简而言之,"type":""
之后的所有记录。任何人都可以提供帮助。
提前致谢。
预期输出
caseOne = [
{"name":"Hulk", "gender":"M", ... ... ,"type":"User"},
{"name":"Loky", "gender":"M", ... ... ,"type":"User"},
{"name":"Thore", "gender":"M", ... ... ,"type":"Admin"}
];
caseTwo = [];
答案 0 :(得分:3)
创建一个可重用的函数getIndex()
,它将获取一个数组参数,并使用type: ""
为您提供该对象的最后一个索引。使用此索引,您可以splice()
结果并获得所需的输出
var caseOne = [
{"name":"Felicity", "gender":"F" ,"type":"Admin"},
{"name":"Tony", "gender":"M","type":""},
{"name":"Super Man", "gender":"M","type":""},
{"name":"Wonder Women", "gender":"F" ,"type":""},
{"name":"Hulk", "gender":"M","type":"User"},
{"name":"Loky", "gender":"M" ,"type":"User"},
{"name":"Thore", "gender":"M" ,"type":"Admin"}
];
var caseTwo = [
{"name":"Felicity", "gender":"F" ,"type":"Admin"},
{"name":"Tony", "gender":"M" ,"type":""},
{"name":"Super Man", "gender":"M" ,"type":""},
{"name":"Wonder Women", "gender":"F","type":""},
{"name":"Hulk", "gender":"M","type":"User"},
{"name":"Loky", "gender":"M","type":"User"},
{"name":"Thore", "gender":"M","type":"Admin"},
{"name":"Bat man", "gender":"M","type":""}
];
function getIndex(caseArray){
var arrayIndex;
caseArray.forEach(function(caseOneObj, index){
if(caseOneObj.type === ''){
arrayIndex = index;
}
});
return arrayIndex;
}
var index = getIndex(caseOne);
var resultCaseOne = caseOne.splice(index+1, caseOne.length);
console.log('-----caseOne-----');
console.log(resultCaseOne);
index = getIndex(caseTwo);
var resultCaseTwo = caseTwo.splice(index+1, caseTwo.length);
console.log('-----caseTwo-----');
console.log(resultCaseTwo);
但是,如果你有最后一个数组最有可能type:''
的对象数组,那么你可以从该数组的最后一个对象开始搜索该索引,如下所示:
var caseOne = [
{"name":"Felicity", "gender":"F" ,"type":"Admin"},
{"name":"Tony", "gender":"M","type":""},
{"name":"Super Man", "gender":"M","type":""},
{"name":"Wonder Women", "gender":"F" ,"type":""},
{"name":"Hulk", "gender":"M","type":"User"},
{"name":"Loky", "gender":"M" ,"type":"User"},
{"name":"Thore", "gender":"M" ,"type":"Admin"}
];
var caseTwo = [
{"name":"Felicity", "gender":"F" ,"type":"Admin"},
{"name":"Tony", "gender":"M" ,"type":""},
{"name":"Super Man", "gender":"M" ,"type":""},
{"name":"Wonder Women", "gender":"F","type":""},
{"name":"Hulk", "gender":"M","type":"User"},
{"name":"Loky", "gender":"M","type":"User"},
{"name":"Thore", "gender":"M","type":"Admin"},
{"name":"Bat man", "gender":"M","type":""}
];
function getIndex(caseArray){
var i;
for(i=caseArray.length-1; i>0; i--){
if(caseArray[i].type === ''){
break;
}
}
return i;
}
var index = getIndex(caseOne);
var resultCaseOne = caseOne.splice(index+1, caseOne.length);
console.log('-----caseOne-----');
console.log(resultCaseOne);
index = getIndex(caseTwo);
var resultCaseTwo = caseTwo.splice(index+1, caseTwo.length);
console.log('-----caseTwo-----');
console.log(resultCaseTwo);
答案 1 :(得分:1)
通过这种方式获得这些数组元素将会解决什么问题? 因为在某个元素之后,搜索数组以找到所有内容后,可能会有更好的方法。 无论如何,你;正在寻找像:
这样的代码
// source data
const source = [
{"name":"Felicity", "gender":"F", "type":"Admin"},
{"name":"Tony", "gender":"M", "type":""},
{"name":"Super Man", "gender":"M", "type":""},
{"name":"Wonder Women", "gender":"F", "type":""},
{"name":"Hulk", "gender":"M", "type":"User"},
{"name":"Loky", "gender":"M", "type":"User"},
{"name":"Thore", "gender":"M", "type":"Admin"}
];
const lastIndex = Array
.from( source ) // Clone the array so reversing does not affect the source.
.reverse() // Reverse the array so we can use findIndex instead of writing findLastIndex ourselves.
.findIndex( entry => !entry.type ); // Get the index of the first element that has no type.
const result = source.slice( source.length - lastIndex, source.length ); // Slice the orginal array.
console.log( result );

答案 2 :(得分:0)
{{1}}