【发布时间】:2015-03-23 02:04:05
【问题描述】:
我是初学者,所以请......温柔点。
代码如下:
for (i = 0; i<importedData.data.length; i++){
var b=importedData.data[i].maker;
console.log(b);
// console.log(importedData.data[i].maker);
var bt = importedData.data[i].body;
console.log(bt);
var ic = importedData.data[i].inkcolor;
console.log(ic);
var pt = importedData.data[i].type;
console.log(pt);
};
我得到了他的结果,但由于有 25 个不同的对象具有相同的字段,因此存在重复。
以下是 JSON 供您查看
exports.data = [
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image01.jpg"
},
{
"type": "fountain",
"maker": "inkwell",
"inkcolor": "blue",
"body": "metal",
"length": "6 inches",
"img" : "image02.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image03.jpg"
},
{
"type": "ballpoint",
"maker": "inkwell",
"inkcolor": "blue",
"body": "ceramic",
"length": "5 inches",
"img" : "image04.jpg"
},
{
"type": "ballpoint",
"maker": "luxor",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image05.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image06.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image07.jpg"
},
{
"type": "ballpoint",
"maker": "montblanc",
"inkcolor" : "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image08.jpg"
},
{
"type": "ballpoint",
"maker": "luxor",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image09.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image10.jpg"
},
{
"type": "ballpoint",
"maker": "inkwell",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image11.jpg"
},
{
"type": "ballpoint",
"maker": "papermate",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image12.jpg"
},
{
"type": "ballpoint",
"maker": "montblanc",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image13.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image14.jpg"
},
{
"type": "fountain",
"maker": "montblanc",
"inkcolor": "black",
"body": "gold",
"length": "6 inches",
"img" : "image15.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "yellow",
"body": "plastic",
"length": "6 inches",
"img" : "image16.jpg"
},
{
"type": "ballpoint",
"maker": "sharpie",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image17.jpg"
},
{
"type": "fountain",
"maker": "papermate",
"inkcolor": "black",
"body": "plastic",
"length": "6 inches",
"img" : "image18.jpg"
},
{
"type": "felt",
"maker": "Bic",
"inkcolor": "blue",
"body": "plastic",
"length": "6 inches",
"img" : "image19.jpg"
},
{
"type": "ballpoint",
"maker": "technicalpen",
"inkcolor": "blue",
"body": "plastic",
"length": "4 inches",
"img" : "image20.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "red",
"body": "metal",
"length": "5 inches",
"img" : "image21.jpg"
},
{
"type": "ballpoint",
"maker": "technicalpen",
"inkcolor": "blue",
"body": "plastic",
"length": "5 inches",
"img" : "image22.jpg"
},
{
"type": "ballpoint",
"maker": "Bic",
"inkcolor": "green",
"body": "plastic",
"length": "6 inches",
"img" : "image23.jpg"
},
{
"type": "ballpoint",
"maker": "parker",
"inkcolor": "black",
"body": "plastic",
"length": "6 inches",
"img" : "image24.jpg"
},
{
"type": "ballpoint",
"maker": "uniball",
"inkcolor": "red",
"body": "plastic",
"length": "6 inches",
"img" : "image25.jpg"
},
];
【问题讨论】:
-
您显示的示例数据有 no 个重复项。也就是说,数组中的每一项都至少有一个与其他项不同的字段。您是说要从数组中删除 项,如果它们是“重复的”,则与其他项具有相同的
type、inkcolor和body(因为这些是您在循环中使用的三个字段)?还有,这和钛有什么关系? -
不太确定你想要什么。你能给出想要的输出吗?是否要跳过包含任何重复字段的整条记录?所有重复的字段?只跳过我们刚刚看到的字段?...
-
重复是什么意思?每个 on 都有不同的 img 字段,但其他字段可能相同。更具体地说明什么构成重复。
-
为任何不一致或遗漏向所有人道歉,因为我是一个新人。背景是钛的一个移动应用项目。
-
@nnnnnn 你是说你想从数组中删除项目,如果它们是“重复的”,因为它们与其他项目具有相同的类型、墨水颜色和正文(因为这三个字段是你在你的循环中使用)?是的,这就是我想要的。
标签: javascript titanium