我正在將一些JSON讀入一個2D JS物件中,如下所示:
我正在將一些JSON讀入一個2D JS物件中。
$.getJSON("file.json"/span>, function(arr) {
dict = arr;
});
當我做 console.log(dict[1]);時的例子--有成千上萬的這樣的行:
{
"ortho"。"a",
"phon": "a",
"lemme": "a",
"cgram": "NOM",
"genre": "m",
"nombre": "",
"freqlemfilms2": "81.36",
"freqlemlivres": "58.65",
"freqfilms2": "81.36",
"_freqlivres": "58.65",
"_infover": "",
"_nbhomogr": "3",
"_nbhomoph": "9",
"_islem": "1",
"_nblettres": "1",
"_nbphons": "1",
"_cvcv": "V",
"_p_cvcv": "V",
"_voisorth": "25",
"_voisphon": "20",
"_puorth": "1",
"_puphon": "1",
"_syll": "a",
"_nbsyll": "1",
"_cv-cv": "V",
"_orthrenv": "a",
"_phonrenv": "a",
"_orthosyll": "a",
"_cgramortho": "NOM,AUX,VER",
"_deflem": ""。
"_defobs": "",
"_old20": "1",
"_pld20": "1",
"_morphoder": "a",
"_nbmorph": "1",
"no": "1", "no".
}
然后,我試圖過濾這一點,以獲得orth是一個特定值的所有行(結果)。
const asArray = Object.entries(dict)。
const result = asArray.filter(span class="hljs-params">word => word。 ortho === 'a')。)
console.log(result)。
這產生了一個空陣列,盡管我知道有幾個結果。 誰能告訴我,我漏掉了什么?謝謝!
uj5u.com熱心網友回復:
問題出在你使用Object. entries()。Object. entries回傳一個[key, value]圖元陣列,所以你的過濾器回呼沒有意義。(它試圖在一個陣列上訪問ortho。[1, {"ortho": "a", "phon": "a", ... }].ortho)
相反,如果dict是一個物件,你需要過濾dict的值。
const dict = { 1: {"ortho" : "a"/span>, "phon"/span>: "a", }, 2: {"ortho": "a"/span>, "phon"/span>: "a",},}.
const result = Object.values(dict)。 filter(word => word.ortho =='a') 。
const dict = { 1: { ortho: 'a', phon: 'a', lemme: 'a', cgram: 'NOM', genre: 'm', nombre: ''/span>, }, 2: { ortho: 'g', phon: 'a', lemme: 'a', cgram: 'NOM', genre: 'm', }, 3: { ortho: 'b', phon: 'a', lemme: 'a', cgram: 'NOM', }, };
const result = Object.values(dict)。 filter((word) => word.ortho =='a'/span>) 。
console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
我只是從你的推論中猜到dict是一個物件,如果它實際上是一個陣列,那么就直接過濾它吧。
const dict = [ {"ortho": "a"/span>, "phon"/span>: "a"/span>, }, {"ortho"/span>: "a"/span>, "phon"/span>: "a",},] 。
const result = dict.filter((word) => word。 ortho === 'a')。)
const dict = [ { ortho: 'a', phon: 'a', lemme: 'a', cgram: 'NOM', genre: 'm', nombre: '' }, { ortho: 'a', phon: 'a', lemme: 'a', cgram: 'NOM', genre: 'm' }, { ortho: 'b', phon: 'a', lemme: 'a', cgram: 'NOM' },] 。
const result = dict.filter((word) => word。 ortho === 'a')。)
console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
編輯(針對您的評論:通過陣列進行過濾)
如果你想過濾多個值,你可以宣告一個陣列,并使用Array#includes()來檢查該值是否被包含。
const dict = [ {"ortho": "a"/span>, "phon"/span>: "a"/span>, }, {"ortho"/span>: "a"/span>, "phon"/span>: "a",},] 。
const filter_array = ['a'/span>, 'c'/span>];
const result = dict.filter((word) => /span> filter_array. includes(word.ortho) )。
const dict = [
{ ortho: 'a', phon: 'a', lemme: 'a', cgram: 'NOM', genre: 'm', nombre: '' },
{ ortho: 'b', phon: 'b', lemme: 'a', cgram: 'NOM', genre: 'm' },
{ ortho: 'c', phon: 'c', lemme: 'a', cgram: 'NOM' },
{ ortho: 'd', phon: 'd', lemme: 'a', cgram: 'NOM' },
];
const filter_array = ['a'/span>, 'c'/span>];
const result = dict.filter((word) => /span> filter_array. includes(word.ortho) )。
console.log(result);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
或者,如果你想指定屬性,你可以宣告一個[key, value]陣列,在你的過濾器呼叫中進行檢查。這里使用Array#some(),如果發現任何匹配的元素,就會進行匹配,但是如果你想只找到符合所有條件的元素,你可以用Array#every()代替它。
const dict = [ {"ortho": "a"/span>, "phon"/span>: "a"/span>, }, {"ortho"/span>: "a"/span>, "phon"/span>: "a",},] 。
const filter_tuples = [['ortho', 'a'],['phon', 'b'], ] 。
const result = dict.filter((word) =>/span> filter_tuples。 some(([k, v]) => word[k] ==v) )。
const dict = [
{ ortho: 'a', phon: 'a', lemme: 'a', cgram: 'NOM', genre: 'm', nombre: '' },
{ ortho: 'b', phon: 'b', lemme: 'a', cgram: 'NOM', genre: 'm' },
{ ortho: 'c', phon: 'c', lemme: 'a', cgram: 'NOM' },
{ ortho: 'd', phon: 'd', lemme: 'a', cgram: 'NOM' },
];
const filter_tuples = [['ortho', 'a'], ['phon', 'b'],]。
const result = dict.filter((word) => filter_tuples。 some(([k, v]) => word[k] == v) )。
console.log(result);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312754.html
標籤:
下一篇:無法訪問物件陣列中的特定資料
