var overallResults: {
John {
Head: 'Positive',
Eye: 'Positive',
Hearing: 'Positive',
Respiratory: 'Positive'
}
}
我要打印: 姓名:约翰,问题:负责人,结果:'积极'
我正在尝试使用:
for(var i=0;i<array.length;i++){
array[i]="#"+array[i];
}
我做错了什么?
答案 0 :(得分:1)
var overallResults = {
John : {
Head: 'Positive',
Eye: 'Positive',
Hearing: 'Positive',
Respiratory: 'Positive'
}
}
var result = {},
key = Object.keys(overallResults)[0],
values = Object.values(overallResults)[0];
result['name'] = key;
for(var i = 0; i < Object.keys(values).length; i++) {
result[Object.keys(values)[i]] = Object.values(values)[i];
}
console.log(result);
Your input was not in correct format. However, I've added the snippet with the correct input. Now you will be able to get all values. Suppose you want to show the answer of 'Eye'. Just print result['Eye'] to do that.
答案 1 :(得分:0)
您可以使用Key
和Value
对来遍历数组。
window.onload = function() {
var overallResults = {
John: {
Head: "Positive",
Eye: "Positive",
Hearing: "Positive",
Respiratory: "Positive"
}
}
//Name: John, Question: Head, Result: 'Positive'
var secondArray = {};
for (let [key, value] of Object.entries(overallResults)) {
console.log(key, value);
secondArray["Name"] = key;
for (let [k, v] of Object.entries(value)) {
console.log(k, v);
secondArray[k] = v;
}
}
console.log("Second Array");
console.log(secondArray);
}
答案 2 :(得分:0)
经过几次测试我就知道了!谢谢:)
import { Injectable } from '@angular/core';
import { AngularFirestore } from "angularfire2/firestore";
@Injectable()
export class MarkersProvider {
constructor(private afs: AngularFirestore) {
}
saveMarker(coords) {
this.afs
.collection('markers')
.add({
latitude: coords.latitude,
longitude: coords.longitude,
message: "hello",
})
.then(() => {
alert('Added');
});
}
getAllMarkers() {
return this.afs.collection('markers').valueChanges();
}
}