다음과 같은 다차원 배열이 있습니다.
[
Object {href="http://www.somepath.com/car4.png", title="Wheel"},
Object {href="http://www.somepath.com/car.png", title="Top"},
Object {href="http://www.somepath.com/car1.png", title="Side"},
Object {href="http://www.somepath.com/car5.png", title="Saddle"},
Object {href="http://www.somepath.com/car6.png", title="Front"}
]
이 개체를 반복하고 쉼표로 구분 된 두 개의 문자열을 검색하고 싶습니다. 하나는 모든 href와 하나는 모든 제목입니다. 그래서 내가 추구하는 것은 :
hrefs = "'http://www.somepath.com/car4.png', 'http://www.somepath.com/car.png', 'http://www.somepath.com/car1.png', 'http://www.somepath.com/car5.png', 'http://www.somepath.com/car6.png'";
titles = "'Wheel', 'Top', 'Side', 'Saddle', 'Front'";
꽤 쉬워 보이지만 지식이 부족하고 구체적인 답을 찾을 수없는 것 같습니다.
map
값을 배열로 가져온 다음 결합 하는 데 사용하는 간단한 함수입니다 .
function createString(arr, key) {
return arr.map(function (obj) {
return "'" + obj[key] + "'";
}).join(', ');
}
// pass in the array and the key the values of which
// you want to 'stringify'
var hrefs = createString(arr, 'href');
var titles = createString(arr, 'title');
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다