Javascript Js:按它对对象数组排序';s值

Javascript Js:按它对对象数组排序';s值,javascript,arrays,object,Javascript,Arrays,Object,所以我有一系列的食物 <label for="search">Search</label><input type="search" name="search" id="search"/> <label>Sort items by <select id="sortby" name="sortby"> </select> <

所以我有一系列的食物

        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
例如:

    const meals = [
{
    id: 1,
    title: 'Strawberry Salad with Poppy Seed Dressing',
    img: 'Strawberry-Salad-with-Poppy-Seed-Dressing.jpg',
    book: 1,
    calories: 298,
    servings: 3,
    type: 'lunch',
    price: 15,
    cook: 'Jenny Jefferson',
    quantity: 10,
},
{
    id: 2,
    title: 'Cashew Turkey Salad Sandwiches',
    img: 'turkey-sandwich.jpg',
    book: 2,
    calories: 198,
    servings: 2,
    type: 'lunch',
    price: 9,
    cook: 'Jenny Jefferson',
    quantity: 10
}];
        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
现在我需要能够排序的id,标题,img,书籍,卡路里,服务,类型,价格,烹饪,质量

        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
我用一个下拉列表来加载这些项目

    function loadDropdownFilter() {
let sortby = document.querySelector("#sortby");
let i = 0;
let filterObj = Object.getOwnPropertyNames(meals[i]);
while (i < filterObj.length) {
    sortby.innerHTML += `<option>${filterObj[i]}</option>`;
    i++;
}
    } 
        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
我试图将这些显示到我的HTML中,但它不起作用

        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
我的代码:

        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
let sortValue = document.querySelector("#sortby").value;
console.log(sortValue);
meals.sort(function(a, b) {
    if (a[sortValue] < b[sortValue]) {
        return -1;
    }
    return 1;
});
console.log(meals);

let id = 1;
for (let i = 0; i < meals.length; i++) {
    let item = meals.find(item => item.id === id);
    document.querySelector("#fillMealList").innerHTML =
        `<article class='createPopup'>
            <h3>${item.title}</h3>
            <figure>
            <img src='images/${item.img}'>
            <figcaption>
            Meal by:<span>${item.cook}</span>
            </figcaption>
            </figure>
            <div class='info'>
            <p>€ <span>${item.price}</span>/pp</p>
            <a href='javascript:addToCart(${item.id})' class='addToCart'>Order</a>
            </div>
            </article>`;
    id++;
}
让sortValue=document.querySelector(“#sortby”).value;
console.log(sortValue);
膳食.分类(功能(a,b){
if(a[sortValue]item.id==id);
document.querySelector(“#fillMealList”).innerHTML=
`
`;
id++;
}

我制作了一个函数来传递到您的
.sort()
语句中,还有一些改进:

        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>
function sortByObject(){
    let sortValue = document.querySelector("#sortby").value;
    console.log(sortValue);
    meals.sort(function(a, b) {
        if (a[sortValue] < b[sortValue]) {
            return -1;
        } 
        return 1;
    })
    console.log(meals);
    meals.forEach(meal => console.log(meal.title));
    var display = document.getElementById("fillMealList");
    meals.forEach(meal = display.innerHTML += meal.title + "<br>");
}
函数sortByObject(){
让sortValue=document.querySelector(“#sortby”).value;
console.log(sortValue);
膳食.分类(功能(a,b){
if(a[sortValue]console.log(fine.title));
var display=document.getElementById(“fillMealList”);
forEach(fine=display.innerHTML+=fine.title+“
”; }

希望这有帮助

您可以在
.sort()
函数中传递一个接受两个参数的函数,从中可以使用您想要的任何字段。i、 e.
let field='title';sort((a,b)=>{if(a[field]
排序不会更改餐点的ID,只更改它们在数组中的位置。尝试用
餐食[i]
替换
餐食。查找(item=>item.id==id)
,应该可以正常工作。(哦,然后你也可以去掉未使用的
id
变量。)谢谢你,这很有效!现在我需要显示这顿饭的标题。你也能帮我吗?没问题-现在编辑有问题的代码到
console.log()
第一顿饭的标题,它被排序到顶部。我的意思是我必须在html中显示所有标题,当我将排序从ID更改为标题时,它必须更改,…哦,对了。现在修复了。我在回答中再次说明了这一点-这将把每个标题附加到

        <label for="search">Search</label><input type="search"   name="search" id="search"/>
        <label>Sort items by
            <select id="sortby" name="sortby">
            </select>
        </label>

        <select id="direction" name="direction">
            <option value="asc">ascending</option>
            <option value="desc">descending</option>
        </select>

    </form>
    <div id="mealList">
        <div class="flexcontainer" id="fillMealList"></div>
    </div>