public int posOfLastNine() {
int lastNineIndex = -1;
int currentNodeIndex = 0;
Node currentNode = first;
while (true) {
if (currentNode == null) {
break;
}
if (currentNode.item == 9.0) {
lastNineIndex = currentNodeIndex;
}
currentNodeIndex++;
currentNode = currentNode.next;
}
return lastNineIndex;
}
这里,测试中的元素可以是,并且位置中不存在的任何元素都应忽略,因为let buckets = [
{ first: { fname: "David", locations: ["q1,""q2,"q3","q4"] } },
{ second: { fname: "Eric", locations: ["a1","a2","a3","a4"] } },
];
test : ["a1","q2","q4","w100"];
不存在,因此需要忽略
对于最终输出,我需要以下内容:
由于测试的第一个元素属于第二个对象的位置,因此我需要将输出显示为:
w100
由于测试的第二个元素属于位置形式的第一个对象,因此我需要将输出显示为:
{
fname: "Eric",
testing: "a1",
name: "copying a1"
},
第三个元素也有相同的规则:
{
fname: "David",
testing: "q2",
name: "copying q2"
}
const buckets = [{
first: {
fname: "David",
locations: ["q1", "q2", "q3", "q4"]
}
},
{
second: {
fname: "Eric",
locations: ["a1", "a2", "a3", "a4"]
}
},
]
const test = ["a1", "q2", "q4", "w100"];
//must take in objects you suspect test values derive from
function compare(obj1, obj2, test) {
//reduce each test ele into an initial empty array output
const output = test.reduce((arr, test) => {
//if test ele belongs to first obj
if (obj1.locations.indexOf(test) > -1) {
//add the following to output
arr.push({
fname: obj1.fname,
testing: test,
name: `copying ${test}`
})
//if test ele belongs to second obj
} else if (obj2.locations.indexOf(test) > -1) {
//add the following to output
arr.push({
fname: obj2.fname,
testing: test,
name: `copying ${test}`
})
}
//repeat cycle
return arr;
}, [])
return output;
}
//pass in the nested objects, not the root objects
console.log(compare(buckets[0].first, buckets[1].second, test));
您的问题对我来说还不是很清楚,但我会试一下:
let buckets = [{
first: {
fname: "David",
locations: ["q1", "q2", "q3", "q4"]
}
}, {
second: {
fname: "Eric",
locations: ["a1", "a2", "a3", "a4"]
}
}, ];
const test = ["a1", "q2", "q4", "w100"];
// the "first" and "second" keys are not used for anything
const flatBuckets = buckets.map(e => {
return Object.values(e)[0]
})
// creating the result:
const result = test.map(e => {
const c = flatBuckets.find(el => {
return el.locations.includes(e)
})
return c ? {
fname: c.fname,
testing: e,
name: `copying ${ e }`
} : null
}).filter(e => e)
console.log(result)