의 내가 이런 구조를 가진 개체가 있다고 가정 해 봅시다 배열로 객체의 수천을 밀어 끝 :루프가 실수로
나는 최소 및 최대 날짜 값을 찾기 위해하는 것입니다 노력하고있어var costByScn = [
{
"key": "K1204",
"values": [
{
"key": "Both",
"values": [
{
"key": "1420070400000",
"values": 27927.7349421797
},
...
]
},
...
]
},
{
"key": "tere",
"values": [
{
"key": "On-Prem",
"values": [
{
"key": "1438387200000",
"values": 1602.24390394729
},
...
]
},
...
]
}
]
, 모든 어레이를 거쳐 본질적으로 함께 동기화 할 수 있습니다. 내 분 1420070400000이며 내 최대가 1,575,158,400,000 (월별 증가), 그것은 지금 values
의 0
에 누락 된 쌍을 생성해야하는 경우
그래서, 나는의 무리와 함께이가 기본적으로 한 이 작업을 시도하는 루프 (이 작업을 수행하는보다 효율적인 방법이 있다면 궁금 할 것입니다.) 내가 지금 할 노력하고있어 만들고 dtMin
및 dtMax
사이에 월별로 증가 밀리 초 타임 스탬프의 dtRng
을 arrary하고 그 증가의 각 배열의 객체를 가지고 있는지 확인입니다
.
나는이 부분이 scnFtprntDates.values.push({key: dtRng[q], values: 0})
에 가깝다 고 생각한다. 배열에 수천 개의 새로운 객체를 넣고 브라우저를 계속 충돌시키는 이유를 알 수 없다. 그것은 무한 루프처럼 어떤 곳을 보이지만 그것은 콘솔 로그로 무엇인지 알아낼 수있는 것은 보여줍니다이를 위해 :
var scn = costByScn
for (var i=0; i<scn.length; i++) {
var dtMax = 0;
var dtMin = 1923273544000;
var scnFtprnt = scn[i];
console.log("scnftprnt: ", scnFtprnt);
console.log("scnftprnt: ", scnFtprnt.values.length);
for (j=0; j<scnFtprnt.values.length; j++) {
var scnFtprntDates = scnFtprnt.values[j];
for (var q=0; q<scnFtprntDates.values.length; q++) {
var scnFtprntDt = scnFtprntDates.values[q].key
if (dtMax < scnFtprntDt) {
dtMax = scnFtprntDt
}
if (dtMin > scnFtprntDt) {
dtMin = scnFtprntDt
}
}
}
var dtRng = []
console.log("dtmin: ", dtMin);
console.log("dtmax: ", dtMax);
var offset = 5*60*60000
dtMin = new Date(+dtMin + offset);
dtMax = new Date(+dtMax + offset);
console.log("dtmin: ", dtMin);
console.log("dtmax: ", dtMax);
while (dtMin <= dtMax) {
dtRng.push(dtMin.getTime() - offset);
dtMin = new Date(new Date(dtMin).setMonth(dtMin.getMonth()+1));
}
console.log("dt rng:", dtRng);
console.log("dt rng:", dtRng.length);
for (l=0; l<scnFtprnt.values.length; l++) {
console.log("scnftprn: ", scnFtprnt.values[l]);
var scnFtprntDates = scnFtprnt.values[l];
if (scnFtprntDates.values.isArray) { console.log("is array")} else { console.log(scnFtprntDates.values.isArray)}
console.log("array2: ", scnFtprntDates.values)
for (var q=0; dtRng.length; q++) {
for (var z=0; z<scnFtprntDates.values.length; z++) {
if (dtRng[q] == scnFtprntDates.values[z].key) {
} else {
console.log("pushing");
scnFtprntDates.values.push({key: dtRng[q], values: 0})
}
}
}
}
}
기본적으로 각 개체의 키 : 여기
dtMin: 1420070400000
dtMax: 1575158400000
dtRng: [1420070400000, 1422748800000, 1425168000000, 1427842800000, 1430434800000, 1433113200000, 1435705200000, 1438383600000, 1441062000000, 1443654000000, 1446332400000, 1448928000000, 1451606400000, 1454284800000, 1456790400000, 1459465200000, 1462057200000, 1464735600000, 1467327600000, 1470006000000, 1472684400000, 1475276400000, 1477954800000, 1480550400000, 1483228800000, 1485907200000, 1488326400000, 1491001200000, 1493593200000, 1496271600000, 1498863600000, 1501542000000, 1504220400000, 1506812400000, 1509490800000, 1512086400000, 1514764800000, 1517443200000, 1519862400000, 1522537200000, 1525129200000, 1527807600000, 1530399600000, 1533078000000, 1535756400000, 1538348400000, 1541026800000, 1543622400000, 1546300800000, 1548979200000, 1551398400000, 1554073200000, 1556665200000, 1559343600000, 1561935600000, 1564614000000, 1567292400000, 1569884400000, 1572562800000, 1575158400000]
내 코드입니다 변수 값 key.values.values는 배열
dtRng
에있는 값 중 하나와 일치해야합니다. 하자 ... 그것은 그 배열에
{key: 1483228800000, values: 0}
같은 개체를 추가해야한다는
dtRng
배열의 값
1483228800000
객체의 key.values.values 배열의
key
에서 찾을 수 없습니다라고
--- 수정 ----
내가 그것을 루프 누락 된 키를 발견하고이 같은 배열로 밀어 부분에 노력 :하지만 그들에게 나는이 결과를 얻을
var lngth = scnFtprntDates.values.length
for (var q=0; q<dtRng.length; q++) {
for (var z=0; z<lngth; z++) {
if (dtRng[q] == parseInt(scnFtprntDates.values[z].key)) {
break;
} else {
console.log("pushing")
scnFtprntDates.values.push({key: dtRng[q], values: 0})
}
}
}
를, 아직 (값을 values: 0
의 사람을 밀어 보인다) 배열에 이미 있습니다 ...
[
{
"key": "Kallam 1204",
"values": [
{
"key": "Both",
"values": [
{
"key": "1420070400000",
"values": 27927.7349421797
},
{
"key": "1422748800000",
"values": 27927.7349421797
},
{
"key": "1425168000000",
"values": 27927.7349421797
},
...
{
"key": 1422748800000,
"values": 0
},
{
"key": 1425168000000,
"values": 0
}
]
}
]
}
]
뿐만 아니라 jsfiddle의 코드와 테스트 데이터의 복사본입니다, https://jsfiddle.net/Lhrwv09p/1/
var costByScn = [{
"key": "K1204",
"values": [{
"key": "Both",
"values": [{
"key": "1420070400000",
"values": 27927.7349421797
}, {
"key": "1422748800000",
"values": 27927.7349421797
}, {
"key": "1425168000000",
"values": 27927.7349421797
}, {
"key": "1427846400000",
"values": 27927.7349421797
}, {
"key": "1430438400000",
"values": 27927.7349421797
}, {
"key": "1433116800000",
"values": 27927.7349421797
}, {
"key": "1435708800000",
"values": 27927.7349421797
}, {
"key": "1438387200000",
"values": 27927.734942179697
}, {
"key": "1441065600000",
"values": 27927.7349421797
}, {
"key": "1443657600000",
"values": 27927.7349421797
}, {
"key": "1446336000000",
"values": 36929.2001421797
}, {
"key": "1448928000000",
"values": 36929.2001421797
}, {
"key": "1451606400000",
"values": 36929.2001421797
}, {
"key": "1454284800000",
"values": 36929.200142179696
}, {
"key": "1456790400000",
"values": 36929.2001421797
}, {
"key": "1459468800000",
"values": 36929.200142179696
}, {
"key": "1462060800000",
"values": 36929.200142179696
}, {
"key": "1464739200000",
"values": 36929.200142179696
}, {
"key": "1467331200000",
"values": 36929.2001421797
}, {
"key": "1470009600000",
"values": 36929.2001421797
}, {
"key": "1472688000000",
"values": 36929.2001421797
}, {
"key": "1475280000000",
"values": 36929.2001421797
}, {
"key": "1477958400000",
"values": 36929.200142179696
}, {
"key": "1480550400000",
"values": 36929.200142179696
}, {
"key": "1483228800000",
"values": 36929.200142179696
}, {
"key": "1485907200000",
"values": 36929.200142179696
}, {
"key": "1488326400000",
"values": 36929.200142179696
}]
}, {
"key": "Cloud",
"values": [{
"key": "1420070400000",
"values": 289819.9054
}, {
"key": "1422748800000",
"values": 289819.9054
}, {
"key": "1425168000000",
"values": 289819.9054
}, {
"key": "1427846400000",
"values": 289819.9054
}, {
"key": "1430438400000",
"values": 289819.9054
}, {
"key": "1433116800000",
"values": 289819.9054
}, {
"key": "1435708800000",
"values": 289819.9054
}, {
"key": "1438387200000",
"values": 289819.9054
}, {
"key": "1441065600000",
"values": 289819.9054
}, {
"key": "1443657600000",
"values": 289819.9054
}, {
"key": "1446336000000",
"values": 289819.9054
}, {
"key": "1448928000000",
"values": 289819.9054
}, {
"key": "1451606400000",
"values": 289819.9054
}, {
"key": "1454284800000",
"values": 289819.9054
}, {
"key": "1456790400000",
"values": 289819.9054
}, {
"key": "1459468800000",
"values": 289819.9054
}, {
"key": "1462060800000",
"values": 289819.9054
}, {
"key": "1464739200000",
"values": 289819.9054
}, {
"key": "1467331200000",
"values": 366563.39859999996
}, {
"key": "1470009600000",
"values": 366563.39859999996
}, {
"key": "1472688000000",
"values": 366563.39859999996
}, {
"key": "1475280000000",
"values": 366563.39859999996
}, {
"key": "1477958400000",
"values": 366563.39859999996
}, {
"key": "1480550400000",
"values": 366563.39859999996
}, {
"key": "1483228800000",
"values": 366563.39859999996
}, {
"key": "1485907200000",
"values": 366563.39859999996
}, {
"key": "1488326400000",
"values": 366563.39859999996
}, {
"key": "1491004800000",
"values": 375564.8638
}, {
"key": "1493596800000",
"values": 375564.8638
}, {
"key": "1496275200000",
"values": 375564.8638
}, {
"key": "1498867200000",
"values": 375564.86380000005
}, {
"key": "1501545600000",
"values": 375564.86380000005
}, {
"key": "1504224000000",
"values": 375564.86380000005
}, {
"key": "1506816000000",
"values": 375564.8638
}, {
"key": "1509494400000",
"values": 375564.86380000005
}, {
"key": "1512086400000",
"values": 375564.86380000005
}, {
"key": "1514764800000",
"values": 499244.18139999994
}, {
"key": "1517443200000",
"values": 499244.1814
}, {
"key": "1519862400000",
"values": 499244.1814
}, {
"key": "1522540800000",
"values": 499244.18139999994
}, {
"key": "1525132800000",
"values": 499244.1814
}, {
"key": "1527811200000",
"values": 499244.1814
}, {
"key": "1530403200000",
"values": 499244.1814
}, {
"key": "1533081600000",
"values": 499244.18139999994
}, {
"key": "1535760000000",
"values": 499244.1814
}, {
"key": "1538352000000",
"values": 499244.1814
}, {
"key": "1541030400000",
"values": 499244.1814
}, {
"key": "1543622400000",
"values": 499244.1814
}, {
"key": "1546300800000",
"values": 499244.1814
}, {
"key": "1548979200000",
"values": 499244.1814
}, {
"key": "1551398400000",
"values": 499244.1814
}, {
"key": "1554076800000",
"values": 499244.1814
}, {
"key": "1556668800000",
"values": 499244.1814
}, {
"key": "1559347200000",
"values": 499244.1814
}, {
"key": "1561939200000",
"values": 499244.18139999994
}, {
"key": "1564617600000",
"values": 499244.1814
}, {
"key": "1567296000000",
"values": 499244.18139999994
}, {
"key": "1569888000000",
"values": 499244.18139999994
}, {
"key": "1572566400000",
"values": 499244.1814
}, {
"key": "1575158400000",
"values": 499244.18139999994
}]
}]
}]
var scn = costByScn
for (var i = 0; i < scn.length; i++) {
var dtMax = 0;
var dtMin = 1923273544000;
var scnFtprnt = scn[i];
console.log("scnftprnt: ", scnFtprnt);
console.log("scnftprnt: ", scnFtprnt.values.length);
for (j = 0; j < scnFtprnt.values.length; j++) {
var scnFtprntDates = scnFtprnt.values[j];
for (var q = 0; q < scnFtprntDates.values.length; q++) {
var scnFtprntDt = scnFtprntDates.values[q].key
if (dtMax < scnFtprntDt) {
dtMax = scnFtprntDt
}
if (dtMin > scnFtprntDt) {
dtMin = scnFtprntDt
}
}
}
var dtRng = []
console.log("dtmin: ", dtMin);
console.log("dtmax: ", dtMax);
var offset = 5 * 60 * 60000
dtMin = new Date(+dtMin + offset);
dtMax = new Date(+dtMax + offset);
console.log("dtmin: ", dtMin);
console.log("dtmax: ", dtMax);
while (dtMin <= dtMax) {
dtRng.push(dtMin.getTime() - offset);
dtMin = new Date(new Date(dtMin).setMonth(dtMin.getMonth() + 1));
}
console.log("dt rng:", dtRng);
console.log("dt rng:", dtRng[0]);
for (l=0; l<scnFtprnt.values.length; l++) {
console.log("scnftprn: ", scnFtprnt.values[l]);
var scnFtprntDates = scnFtprnt.values[l];
var scn2 = scnFtprntDates.values
if (scn2.isArray) { console.log("is array")} else { console.log("is not array")}
console.log("array2: ", scnFtprntDates.values)
var lngth = scnFtprntDates.values.length
for (var q=0; q<dtRng.length; q++) {
console.log("part1: ", dtRng[q].toString());
if (_.some([scn2], ["key", dtRng[q]])) {
} else {
scn2.push({key: dtRng[q], values: 0})
}
}
}
}
console.log("data2: ", JSON.stringify(scn));
은 _ "지금, 나는이 작업을 수행하려고 루프의 무리와 함께이가 기본적으로했지만이뿐만 아니라이 일을 더 효율적인 방법입니다?"_ 당신은 할 수 [Lodash] (https://lodash.com) –
을 사용하십시오. 여기에 배열과 객체를 중첩 했으므로 결과를 어떻게 보이게할지 명확하지 않습니다. 예전과 후의 예를 명확하게 제시 할 수 있습니까? – JLRishe
@JLRishe 나는 최후에 편집을 시도하고 설명하기 위해 ... – user2061886