假设我有一个像这样的数组:
arrayOfObject = [{item: {this: is, that: is}}, {item2: {this: is, that: is}}]
我试图访问item和item2,而不必使用0/1索引。我希望能够说arrayOfObjects [item]进入对象。这可能吗?
答案 0 :(得分:1)
您可以使用Array.find。
arrayOfObject = [{
item: {
this: 'is',
that: 'is'
}
}, {
item2: {
this: 'is',
that: 'is'
}
}]
console.log(arrayOfObject.find(ob => ob['item']));
console.log(arrayOfObject.find(ob => ob['item2']));
答案 1 :(得分:1)
public class Problem {
public void actions() {
GUI guiObject = new GUI();
if(guiObject.getDifficulty() == 0) {
System.out.println("Easy");
}
else if(guiObject.getDifficulty() == 1) {
System.out.println("Middle");
}
else if(guiObject.getDifficulty() == 2) {
System.out.println("Hard");
}
}
}
答案 2 :(得分:0)
是的,肯定有可能:
for f in test.log.*; do echo mv "$f" "$f.$(date -I)"; done
或
var result = arrayOfObject.map(a => a.item);
答案 3 :(得分:0)
您不能完全做到这一点,但可以通过类似的操作将数组“转换”为对象,然后使用键访问值:
arrayOfObject = [{ item: { this: "a", that: "b" } }, { item2: { this: "c", that: "d" } }]
const arrayToObject = arrayOfObject.reduce((r,c) => Object.assign(r,c), {})
console.log(arrayToObject['item'])
console.log(arrayToObject['item2'])
在上面的代码段中,我们将arrayOfObject
转换为数组 To 对象,然后只需通过键即可访问值。
否则,您将无法执行操作,因为您只能按索引或通过某种可以遍历并获取条目的函数(例如find
等)访问数组中的值。< / p>