홈>
일부 데이터를 가져오고 있습니다. 이 데이터를 키별로 그룹화 한 다음 Vue의 v-for 루프에서 인쇄하려고합니다.
코드는 다음과 같습니다 :
// I initialize an empty array in data
data() {
return {
locArray: []
}
}
// This is done in the api call, in beforeRouteEnter()
const items = data.continental;
Array.prototype.groupBy = function(prop) {
return this.reduce(function(groups, item) {
const val = item[prop];
groups[val] = groups[val] || [];
groups[val].push(item);
return groups;
}, {});
};
for (let i in items) {
let source = items[i];
let details = source.details;
let groupedByLocation = details.groupBy(location);
vm.locArray = groupedByLocation
}
// This is an example of what the data looks like
{
data: {
continental: {
countryOne: {
weather: "weatherOne",
details: [{
location: "west",
foods: "foodOne",
mainCities: [
"cityOne",
"cityTwo",
"cityThree"
]
}]
},
countryTwo: {
weather: "weatherTwo",
details: [{
location: "north",
foods: "foodTwo",
mainCities: [
"cityOne",
"cityTwo",
"cityThree"
]
}]
},
countryThree: {
weather: "weatherThree",
details: [{
location: "north",
foods: "foodThree",
mainCities: [
"cityOne",
"cityTwo",
"cityThree"
]
}]
},
countryFour: {
weather: "WeatherFour",
details: [{
location: "west",
foods: "foodFour",
mainCities: [
"cityOne",
"cityTwo",
"cityThree"
]
}]
},
countryfive: {
weather: "WeatherFive",
details: [{
location: "north",
foods: "foodFive",
mainCities: [
"cityOne",
"cityTwo",
"cityThree"
]
}]
}
}
}
}
<div class="entry-content">
<div class="single-entry" v-for="loc in locArray" :key="loc.id">
<span class="test-wrap">{{ loc }}</span>
</div>
</div>
내가
console.log(groupedByLocation)
때
표시 해야하는 모든 데이터를 얻지 만 v-for에서는 배열의 마지막 객체 만 가져옵니다.
간단 해 보이지만 정말 혼란 스러워요.
도움을 주시면 대단히 감사하겠습니다!
The desired outcome is that I'd like to print all the items that have
location: north
위의 그룹과location: west
가있는 모든 항목에 함께 아래 다른 그룹에 있습니다.
- 답변 # 1
관련 자료
- JavaScript에서 객체 배열을 하나의 객체로 어떻게 변환합니까?
- c# - 개체 배열이있는 복잡한 중첩 개체
- javascript - 개체 배열에서 개체 선택
- javascript - 조건에 따라 개체 배열에 를 추가 할 수 없습니다
- javascript - map ()을 사용하여 객체 배열에서 객체를 만들 수 있습니까?
- javascript - 객체 배열에서 처음 반복되는 객체를 얻는 방법은 무엇입니까?
- javascript - 객체 배열 안에 객체의 하나의 값만 놓는 방법
- javascript - 객체 객체를 기반으로 객체 배열 생성
- javascript - 객체 배열에서 값이 반복되는 횟수
- 객체 목록을 반환하는 Java Stream of Integer 배열
- powershell - 문자열을 Object 배열로 분할
- html - Array Javascript의 일부 객체에서 임의의 항목 가져 오기
- javascript - JS에서 피보나치 시퀀스를 만들 때 배열의 처음 두 항목을 가져올 수 없습니다
- c++ - 포인터 배열에서 개체 제거
- javascript - React js에서 객체의 배열, 배열 내부의 객체를 증가시키는 방법은 무엇입니까?
- javascript - 요소와 조건을 일치시키기 위해 Set 및 개체를 사용하여 필터 배열 함수 개선
- javascript - 배열에서 다른 개체 배열과 동일한 개체 삭제
- json - 트리 유형 객체 배열 Javascript에서 모든 하위 레코드 가져 오기
- javascript - 키 배열 및 값 배열에서 객체 생성
- javascript - 다른 객체 배열을 기반으로 객체 배열 필터링
관련 질문
- arrays : Angular로 복잡한 json 파싱하기
- arrays : Kotlin -JSON 개체에서 배열의 값 가져오기 android
- javascript : 로컬 스토리지 값이 변경될 때 객체 속성 값을 변경하려면 어떻게 해야 합니까?
- python : list() 객체를 numpy 배열로 변환하는 방법
- JSON 배열로 C# POST 요청을 보내는 방법
- arrays : API는 Google 스크립트의 데이터 스프레드시트에 Json 배열을 요청합니다.
- java : Android에서 JSON의 중첩 배열 내부에 있는 객체에 어떻게 액세스할 수 있습니까?
- 행 제한에 도달한 후 테이블 행을 자동으로 삭제하고 javascript로 Websocket 데이터를 사용하여 행에 새 데이터 추가
- c# : JSON 속성은 객체 또는 배열일 수 있습니다. 데이터를 올바르게 처리하는 방법은 무엇입니까?
- arrays : JQ를 사용하여 값으로 배열 항목 선택
왜 당신이
groupBy
를 부르는지 모르겠습니다 for 루프가 반복 될 때마다details[0].location
로이 필터링을 해결하겠습니다.continental
의 Object.values () :예 :