gpt4 book ai didi

Javascript 数组 - 合并多个数组帮助

转载 作者:行者123 更新时间:2023-12-01 16:01:38 25 4
gpt4 key购买 nike

我想知道是否有人可以为我的 JS 项目遇到的以下问题提供任何帮助?

我基本上有关于从 3 个不同的 JSP 列表中提取产品的信息:

  • 产品详细信息
  • A 产品的评级
  • A 产品的零售商

我想做的是循环遍历 JSP 的三个列表,提取每个产品的相关信息,然后创建一个 Javascript 对象,将所有产品信息保存在一起。

创建后,我需要循环遍历这个 Javascript 对象并在屏幕上显示每个产品的信息 - 一次可能有 10 个、20 个产品等。

我遇到问题的地方是,当我创建“mainResults”对象时(请参阅下面的代码片段),尽管它已正确填充,但我首先得到一个空白数组,然后是产品数组。

下面是 Firebug 的屏幕截图,显示了“mainResults”javascript 对象的摘录 - 这是我的问题:空数组显示:

link text

当我尝试使用其 length 属性循环遍历“mainResults”时,这给我带来了问题,因为它总是返回为“0”。

我确信有更好的方法来实现我需要做的事情,但经过大量搜索和使用 OOP 之后,我仍然找不到好的解决方案。

我需要补充一些重要的事情:

  • 我永远不会确切知道会有多少产品。
  • 每个产品最多有 3 个评分。
  • 我永远无法确切知道某种产品有多少家零售商。
  • 我需要通过索引访问产品、其评级和零售商

我已经在下面发布了代码的相关部分 - 我真的希望有人能给我指出正确的方向:)

提前非常感谢,

吉姆

<body onLoad="populate()">
<jsp:useBean id="products" scope="request" class="com.sslpost.mpr.pricerunner.Products" />

<% if(request.getAttribute("products") != null)
products = (Products)request.getAttribute("products"); %>

<script language="JavaScript" type="text/javascript">
function populate(){
<% if(products != null) { %>

//Access Products from JSP Product Class

<% for (int i=0; i<products.productsList.size(); i++) {
Product currProd = products.productsList.get(i);

//Check Product Id is Not Empty and Handle Accordingly
String prodId = "";
if(products.productsList.get(i).id != 0)
{ prodId = Integer.toString(products.productsList.get(i).id); }
else{ prodId = Integer.toString(0); }

//Check Product Name is Not Empty and Handle Accordingly
String prodName = "";
if(products.productsList.get(i).name != null)
prodName = products.productsList.get(i).name;

//Check Product Manufacturer is Not Empty and Handle Accordingly
String prodManufacturer = "";
if(products.productsList.get(i).manufacturer != null)
prodManufacturer = products.productsList.get(i).manufacturer;

//Check Product Description is Not Empty and Handle Accordingly
String prodDescription = "";
if(products.productsList.get(i).description != null)
prodDescription = products.productsList.get(i).description;

//Check Product Retailer is Not Empty and Handle Accordingly
String prodRetailer = "";
if(products.productsList.get(i).numberOfRetailers != 0)
{ prodRetailer = Integer.toString(products.productsList.get(i).numberOfRetailers); }
else{ prodRetailer = Integer.toString(0); }

//Check Product Logo + Height and Width Attributes are Not Empty and Handle Accordingly
String prodLogo = "";
if(products.productsList.get(i).imageUrl != null && products.productsList.get(i).imageUrl.imageUrl != null)
{ prodLogo = products.productsList.get(i).imageUrl.imageUrl; }
else{ prodLogo = "images/image_remove.png"; }

String prodLogoHeight = "";
if(products.productsList.get(i).imageUrl != null && products.productsList.get(i).imageUrl.height != 0)
{ prodLogoHeight = Integer.toString(products.productsList.get(i).imageUrl.height); }
else{ prodLogoHeight = Integer.toString(0); }

String prodLogoWidth = "";
if(products.productsList.get(i).imageUrl != null && products.productsList.get(i).imageUrl.width != 0)
{ prodLogoWidth = Integer.toString(products.productsList.get(i).imageUrl.width); }
else{ prodLogoWidth = Integer.toString(0); }

//Check Product Prices + Currency Attributes are Not Empty and Handle Accordingly
String lowPriceAmount = "";
if(products.productsList.get(i).lowestPrice != null && products.productsList.get(i).lowestPrice.amount != null)
lowPriceAmount = products.productsList.get(i).lowestPrice.amount;

String lowPriceCurrency = "";
if(products.productsList.get(i).lowestPrice != null && products.productsList.get(i).lowestPrice.currency != null)
lowPriceCurrency = products.productsList.get(i).lowestPrice.currency;

String highPriceAmount = "";
if(products.productsList.get(i).highestPrice != null && products.productsList.get(i).highestPrice.amount != null)
highPriceAmount = products.productsList.get(i).highestPrice.amount;

String highPriceCurrency = "";
if(products.productsList.get(i).highestPrice != null && products.productsList.get(i).highestPrice.currency != null)
highPriceCurrency = products.productsList.get(i).highestPrice.currency;
%>

productResults(<%= i %>,
"<%= prodId %>",
"<%= prodName %>",
"<%= prodManufacturer %>",
"<%= prodDescription %>",
"<%= prodRetailer %>",
"<%= prodLogo %>",
"<%= prodLogoHeight %>",
"<%= prodLogoWidth %>",
"<%= lowPriceAmount %>",
"<%= lowPriceCurrency %>",
"<%= highPriceAmount %>",
"<%= highPriceCurrency %>"
);

//Access Ratings from Ratings List in JSP Product Class

<% for(int j=0; j<currProd.ratingList.size(); j++) {

//Check Product Rating Type is Not Empty and Handle Accordingly
String prodRatType = "";
if(currProd.ratingList.get(j).type != null)
{ prodRatType = currProd.ratingList.get(j).type; }
else{ prodRatType = "no rating"; }

//Check Product Rating Average Value is Not Empty and Handle Accordingly
String prodRatAvg = "";
if(currProd.ratingList.get(j).average != null)
{ prodRatAvg = currProd.ratingList.get(j).average; }
else{ prodRatAvg = Integer.toString(0); }

//Check Number of Product Ratings Value is Not Empty and Handle Accordingly
String prodRatNum = "";
if(currProd.ratingList.get(j).numberRatings != null)
{ prodRatNum = currProd.ratingList.get(j).numberRatings; }
else{ prodRatNum = Integer.toString(0); }

%>

ratingResults(<%= j %>,
<%= i %>,
"<%= prodRatType %>",
"<%= prodRatAvg %>",
"<%= prodRatNum %>"
);
<% } %>

//Access Retailers from Retailer List in JSP Product Class

<% for(int x=0; x<currProd.retailerList.size(); x++) {

//Check Retiailer Logo is Not Empty and Handle Accordingly
String retailLogo = "";
if(currProd.retailerList.get(k).logo != null && currProd.retailerList.get(k).logo.imageUrl != null)
retailLogo = currProd.retailerList.get(k).logo.imageUrl;

//Check Retailer Product Prices + Currency Attributes are Not Empty and Handle Accordingly
String minShipPrice = "";
if(currProd.retailerList.get(k).minPriceWithShipping != null && currProd.retailerList.get(k).minPriceWithShipping.amount != null)
minShipPrice = currProd.retailerList.get(k).minPriceWithShipping.amount;

String minShipCurrency = "";
if(currProd.retailerList.get(k).minPriceWithShipping != null && currProd.retailerList.get(k).minPriceWithShipping.currency != null)
minShipCurrency = currProd.retailerList.get(k).minPriceWithShipping.currency;

String maxShipPrice = "";
if(currProd.retailerList.get(k).maxPriceWithShipping != null && currProd.retailerList.get(k).maxPriceWithShipping.amount != null)
maxShipPrice = currProd.retailerList.get(k).maxPriceWithShipping.amount;

String maxShipCurrency = "";
if(currProd.retailerList.get(k).maxPriceWithShipping != null && currProd.retailerList.get(k).maxPriceWithShipping.currency != null)
maxShipCurrency = currProd.retailerList.get(k).maxPriceWithShipping.currency;

%>

retailerResults(<%= k %>
<%= i %>,
"<%= currProd.retailerList.get(k).id %>",
"<%= currProd.retailerList.get(k).name %>",
"<%= currProd.retailerList.get(k).link %>",
"<%= retailLogo %>",
"<%= currProd.retailerList.get(k).leadTime %>",
"<%= currProd.retailerList.get(k).stockInfo %>",
"<%= currProd.retailerList.get(k).price.amount %>",
"<%= currProd.retailerList.get(k).price.currency %>",
"<%= currProd.retailerList.get(k).sourceType %>",
"<%= currProd.retailerList.get(k).sourceDate %>",
"<%= minShipPrice %>",
"<%= minShipCurrency%>",
"<%= maxShipPrice %>",
"<%= maxShipCurrency %>"
);
<% } %>
<% } %>
<% } %>
}
</script>
<script language="JavaScript" type="text/javascript">
var mainResults = [];

function productResults(pnum, pid, pname, pmanu, pdesc, pretail, plogo, plogoh, plogow, plowamt, plowcur, phghamt, phghcur)
{
var product = [];

product.count = pnum;
product.id = pid;
product.name = pname;
product.manufacturer = pmanu;
product.description = pdesc;
product.numretailer = pretail;
product.logo = plogo;
product.logoh = plogoh;
product.logow = plogow;
product.lowpriceamt = plowamt;
product.lowpricecur = plowcur;
product.highpriceamt = phghamt;
product.highpricecur = phghcur;
product.ratings = [];
product.retailers = [];
mainResults[pnum] = product;
}

function ratingResults(ratnum, pnum, rtgtype, rtgavg, rtgnum)
{
var ratings = [];

ratings.count = ratnum;
ratings.type = rtgtype;
ratings.average = rtgavg;
ratings.number = rtgnum;
mainResults[pnum].ratings[ratnum] = ratings;
}

function retailerResults(retnum, pnum, rid, rname, rlink, rlogo, rtime, rstock, ramt, rcur, rstype, rsdate, rminamt, rmincur, rmaxamt, rmaxcur)
{
var retailers = [];

retailers.count = retnum;
retailers.id = rid;
retailers.name = rname;
retailers.purchaselink = rlink;
retailers.logo = rlogo;
retailers.leadtime = rtime;
retailers.stock = rstock;
retailers.priceamt = ramt;
retailers.pricecur = rcur;
retailers.sourcetype = rstype;
retailers.sourcedate = rsdate;
retailers.shipminprice = rminamt;
retailers.shipmincur = rmincur;
retailers.shipmaxprice = rmaxamt;
retailers.shipmaxcur = rmaxcur;
mainresults[pnum].retailers[retnum] = retailers;
}
</script>

最佳答案

使用 JSP 生成代码是一个糟糕的想法...这绝对是我见过的最糟糕的代码生成。也就是说......让我们看看我们是否可以解决你的困惑......

首先,所有这些函数都是没有意义的,你在jsp中不加区别地输出数据,这些函数实际上只会降低性能。因此,我们最好立即渲染您想要的数组(mainResults)。

<% if(products != null) { %>

var mainResults = [
<% for (int i=0; i<products.productsList.size(); i++) {
Product currProd = products.productsList.get(i);
%>

{
count : "<%= i %>",
id : "<%= prodId %>",
name : "<%= prodName %>",
manufacturer : "<%= prodManufacturer %>",
description : "<%= prodDescription %>",
numretailer : "<%= prodRetailer %>",
logo : "<%= prodLogo %>",
logoh : "<%= prodLogoHeight %>",
logow : "<%= prodLogoWidth %>",
lowpriceamt : "<%= lowPriceAmount %>",
lowpricecur : "<%= lowPriceCurrency %>",
highpriceamt : "<%= highPriceAmount %>",
highpricecur : "<%= highPriceCurrency %>",
ratings : [
<% for(int j=0; j<currProd.ratingList.size(); j++) { %>
{
count : "<%= j %>",
type : "<%= prodRatType %>",
average : "<%= prodRatAvg %>",
number : "<%= prodRatNum %>"
}
<% if(j < (currProd.ratingList.size() - 1)) { %>
,
<% } %>

<% } %>
],
retailers : [
<% for(int x=0; x<currProd.retailerList.size(); x++) { %>
{
retailers.count : "<%= x %>",
id : "<%= currProd.retailerList.get(k).id %>",
name : "<%= currProd.retailerList.get(k).name %>",
purchaselink : "<%= currProd.retailerList.get(k).link %>",
logo : "<%= retailLogo %>",
leadtime : "<%= currProd.retailerList.get(k).leadTime %>",
stock : "<%= currProd.retailerList.get(k).stockInfo %>",
priceamt : "<%= currProd.retailerList.get(k).price.amount %>",
pricecur : "<%= currProd.retailerList.get(k).price.currency %>",
sourcetype : "<%= currProd.retailerList.get(k).sourceType %>",
sourcedate : "<%= currProd.retailerList.get(k).sourceDate %>",
shipminprice : "<%= minShipPrice %>",
shipmincur : "<%= minShipCurrency%>",
shipmaxprice : "<%= maxShipPrice %>",
shipmaxcur : "<%= maxShipCurrency %>"
}

<% if(x < (currProd.retailerList.size() - 1)) { %>
,
<% } %>

<% } %>
]
}

<% if(i < (products.productsList.size() - 1)) { %>
,
<% } %>

<% } %>
];

<% } %>

我可能犯了一些拼写错误,需要做很多工作来修复...但这是一个稍微好一点的设置。

但是看在上帝的份上,请了解如何使用 Java 创建 JSON 并在 Java 中创建模型,然后解析为 JSON。

关于Javascript 数组 - 合并多个数组帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3726840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com