简体   繁体   English

javascript遍历2d数组

[英]javascript looping through 2d array

I am trying to work through how to publish items from multiple arrays while first searching through the separate array elements and then return only those items which match a certain criteria. 我正在尝试解决如何从多个数组发布项目,同时首先搜索单独的数组元素,然后仅返回那些符合特定条件的项目。 The following code does not do this yet. 以下代码尚未执行此操作。

enter code here<div id="myDiv">
<script type="text/javascript">


var $ = function(id) {
    return document.getElementBuId(id);

}
var title = new Array("Barbecuing in the Rain","Picnicing in the Arctic",
        "How to Saute on a Radiator", "A Real Grasshopper: Bartending with Insects",
        "Food with a Bullet:  Cooking in Camden", "Mirepoix for Dummies");
var price = new Array(18.50, 23.95, 41.95, 5.95, 13.00, 99.00);
var binding = new Array("Hardcover", "Paperback", "Hardcover", "Paperback", "Paperback", "Hardcover");
var pubYear = new Array(2002, 1975, 2004, 2006, 2001, 1997);
var NYTBestSeller = new Array(true, true, false, false, true, true)
    var seriesTitle = new Array("Food Network", "The Food Network", 
            "Culinary Shortcuts", "Sunset Libations", 
            "Food Network", "The Food Network");
var bargainBooks = ("Food Network Hot Selling Bargain Book");

for (i in title) {
    if (((price[i] <= 25 && binding[i] == "Hardcover") || 
                (price[i] <= 15 && binding[i] == "Softcover")) && (pubYear[i] >= 2000) && 
            (NYTBestSeller[i] == true) && (seriesTitle[i] == "Food Network") || (seriesTitle[i] == "The Food Network"))


        //document.write("<span color='red'>This line is red</span>");



        document.write(title[i] + "<br />");
    document.write(price[i] + "<br />");
    document.write(binding[i] + "<br />");
    document.write(pubYear[i] + "<br />");
    document.write(NYTBestSeller[i] + "<br />");
    document.write(seriesTitle[i] + "<br />" + "<hr/>");

}
</script>

I am searching for books that met a certain criteria and then trying to output where that book is on the page a message of Hot Selling Book in green. 我正在搜索符合特定条件的书,然后尝试以绿色输出在页面上该书在何处的信息。 But I can't figure out how to make the document.write work. 但是我不知道如何使document.write工作。 document.write().style.color="green" does not work. document.write()。style.color =“ green”不起作用。

document.write output text only, you have to write your own tags there (a div styled as green for example). 仅在document.write输出文本中,您必须在此处编写自己的标签(例如,样式为绿色的div)。

However I suggest you a different approach, what about an empty div with an id and then do the following: 但是,我建议您采用另一种方法,对于具有id的空div呢,然后执行以下操作:

document.getElementById('your-div-id').innerHTML = 'text <br /> <span style="color: green;">other text</span> and so on';

Will also reorganize code in a better way. 还将以更好的方式重组代码。

Document.write will only append code to the end of the output stream (the document in this case), so you can't specify where output will appear. Document.write只会将代码附加到输出流(在本例中为文档)的末尾,因此您无法指定输出将出现的位置。

Edit 1: Also, what about using objects in this case? 编辑1:另外,在这种情况下使用对象呢? As far as I can see, you are using arrays to emulate their behaviour: 据我所知,您正在使用数组来模拟其行为:

var myObjArray = [
    {
        title: 'mytitle1',
        price: '10$',
        binding: 'dunno',
        pubYear: 'dunno-again',
        NYTBestSeller: true,
        seriesTitle: 'Something'
    },
    {
        ...other object data...
    }
]

document.getElementById('your-div-id').innerHTML += myObjArray[0].title + '<br />' + myObjArray[0].price;

Hope you can find it interesting 希望你能发现它有趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM