2016-08-13 10 views
0

私はいくつかの日付と値を持つ配列を持っています。例:日付xには20件の注文があり、日付には32件の注文があります。キーに基づく2つの配列をマップします

[2016-08-09: 38, 2016-08-08: 75, 2016-08-05: 13, 2016-08-04: 23, 2016-08-03: 10] 

第2の配列は、先月からのすべての日付を含む配列です。例:空の場合、私は、この配列をマッピングし、日付を確認しようとした

["2016-07-14", "2016-07-15", "2016-07-16", "2016-07-17", "2016-07-18", "2016-07-19", "2016-07-20", "2016-07-21", "2016-07-22", "2016-07-23", "2016-07-24", "2016-07-25", "2016-07-26", "2016-07-27", "2016-07-28", "2016-07-29", "2016-07-30", "2016-07-31", "2016-08-01", "2016-08-02", "2016-08-03", "2016-08-04", "2016-08-05", "2016-08-06", "2016-08-07", "2016-08-08", "2016-08-09", "2016-08-10", "2016-08-11", "2016-08-12", "2016-08-13"] 

その後、他には、最初の配列で見つかった値を書き込む「」書きます。

 var totalInputOrders = allDates.map(function(date) { 
       return [ date, inputOrdersCount[date] ? "" : inputOrdersCount[date] ]; 
     }); 

それは私の2つの値を持つ配列要素を持つ31の長さの配列を与えます。日付=>値。最初の配列に日付が見つからない場合、 ""が書き込まれますが、最初の配列の値は見つかりません。この問題でどこが間違っていますか?

+1

あなたは、正確な出力を表示してくださいできますあなたは期待している?そして、ちなみに、最初の配列は無効な構文です。これらの配列値を引用する必要があります: '[" 2016-08-09:38 "...]' –

+0

私は期待しています:[[2016-08-10、 ""]、[2016-08-09,38]、[2016-08-08,75]、[2016-08-07、 "]]、[2016-08-06、"]、[2016-08 -05、13]]など... –

+0

三者が間違っているようです。あなたは 'inputOrdersCount [date]のようにしますか? inputOrdersCount [date]: "" 'または'!inputOrdersCount [date]? "":inputOrdersCount [date] ' – Redu

答えて

2

質問へyour commentに基づいて、あなたが期待するように見えるとして、この作品を作るために、まず:

私は期待してい:[[2016-08-10, ""], [2016-08-09, 38], [2016-08-08, 75], [2016-08-07, ""], [2016-08-06, ""], [2016-08-05, 13]]のように...

を私はしたいです

// using an Object, in place of an Array, to associate 
 
// dates with order-counts, and wrapping the dates in 
 
// quotes (otherwise you have invalid syntax [1], hyphens 
 
// cannot be used unquoted in variable names even if 
 
// wrapped in parentheses to attempt to have the result 
 
// of a mathematical expression be used as the key [2]): 
 
var inputOrdersCount = { 
 
    "2016-08-09": 38, 
 
    "2016-08-08": 75, 
 
    "2016-08-05": 13, 
 
    "2016-08-04": 23, 
 
    "2016-08-03": 10 
 
    }, 
 

 
    // no changes to this Array: 
 
    allDates = ["2016-07-14", "2016-07-15", "2016-07-16", "2016-07-17", "2016-07-18", "2016-07-19", "2016-07-20", "2016-07-21", "2016-07-22", "2016-07-23", "2016-07-24", "2016-07-25", "2016-07-26", "2016-07-27", "2016-07-28", "2016-07-29", "2016-07-30", "2016-07-31", "2016-08-01", "2016-08-02", "2016-08-03", "2016-08-04", "2016-08-05", "2016-08-06", "2016-08-07", "2016-08-08", "2016-08-09", "2016-08-10", "2016-08-11", "2016-08-12", "2016-08-13"], 
 

 
    // using Array.prototype.map() to iterate over the allDates 
 
    // Array: 
 
    totalInputOrders = allDates.map(function(date) { 
 
    // 'date', the first argment, is a reference to 
 
    // the current array-element of the Array over 
 
    // which we're iterating. 
 

 
    // here we return an Array with the current 
 
    // array-element as the first array-element 
 
    // of the created-Array, and the second value 
 
    // determined by the existence of a result 
 
    // from the inputOrdersCount Object using 
 
    // the current array-element as the key; if 
 
    // there is a result we use that result, if 
 
    // no result (undefined) then we use an 
 
    // empty string: 
 
    return [date, (inputOrdersCount[date] ? inputOrdersCount[date] : "")]; 
 
    }); 
 

 
// logging the full totalInputOrders Array, and the filtered 
 
// totalInputOrders Array, returning only those Array-elements 
 
// that do not have an empty String as the second array-element: 
 
console.log(totalInputOrders, totalInputOrders.filter(function(arr) { 
 
    return arr[1] !== ""; 
 
}));

次のアプローチを提案します

JS Fiddle demo

さて、あなたが期待どおりコードが機能しなかった理由の:

  1. [2016-08-09: 38, 2016-08-08: 75, 2016-08-05: 13, 2016-08-04: 23, 2016-08-03: 10]これは構文エラー、配列値–であり、これは配列ではなく、オブジェクト–である必要があるだろうそれ以外の場合は、"Uncaught SyntaxError: Unexpected token :"のエラーが発生する可能性があります。

  2. 他の配列に保持されている値にアクセスするためのキーとして数値以外の配列要素を使用しようとすると、アクセスしようとしている配列にアクセスするために未定義の値が返されます。同じ表記ですが、数字のインデックスを使用します(例:inputOrdersCount[0])。"2016-08-09: 38"(配列要素を引用しています)を返します。を検索する場合は、を取得する必要があります。inputOrdersCountはObject

  3. あなたの条件(3値)演算子が間違っています;あなたがするべきことはですinputOrdersCount[date]チェック–から肯定的な結果が得られた場合はその結果を返し、チェックの結果がない場合は空の文字列を返します。残念なことに、あなたのステートメントの順番を考えると、正反対です。チェックが正の場合は空の文字列を返し、未定義の結果を返します(正、真または真実の結果がない場合は同じinputOrdersCount[date]となります)。三元の順序が重要であり、パターンを、次のとおりです。

    assessment ? ifTrue : ifFalse; 
    

    だから、代わりに書かれている必要があります。

    inputOrdersCount[date] ? inputOrdersCount[date] : ""; 
    

参考文献:


脚注:可変名にハイフンを使用して、構文エラーの

  1. 複製、:構文エラーのhttps://jsfiddle.net/davidThomas/842q0s89/1/
  2. 複製、など和の結果を使用しようとするために、括弧を使用してオブジェクトキー:https://jsfiddle.net/davidThomas/842q0s89/2/
  3. エラーの再現、"Uncaught SyntaxError: Unexpected token :"
0

あなたはこのようなreduce使用することができ、inputOrdersCount[date]を行うことができるようにオブジェクトに文字列の配列を変換する必要がある場合:

var inputOrdersCount = ["2016-08-09: 38", "2016-08-08: 75", "2016-08-05: 13", "2016-08-04: 23", "2016-08-03: 10"]; 
 

 
inputOrdersCount = inputOrdersCount.reduce(function(counter, item) { 
 
    var split = item.split(/: ?/); 
 
    counter[split[0]] = parseInt(split[1], 10); 
 
    return counter; 
 
}, {}); 
 

 
console.log(inputOrdersCount);

関連する問題