-2

I am beginner so PLEASE..be gentle.

Here is the code:

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);

};

I get he results but as there are 25 different objects with same fields there are duplicates.

below is the JSON for your viewing

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"
    },
];
Pang
  • 9,564
  • 146
  • 81
  • 122
osa1
  • 1
  • 1
  • 3
    The sample data you show has *no* duplicates. That is, each item in the array has at least one field that is different to the other items. Are you saying you want to *remove* items from the array if they are "duplicates" in the sense of having the same `type`, `inkcolor` and `body` as other items (since those are the three fields you use in your loop)? Also, what does this have to do with titanium? – nnnnnn Mar 23 '15 at 02:09
  • Not quite sure what you want. Can you give the desired output? Do you want to skip the entire record that has any duplicate field? All duplicate fields? Skip just the fields we've just seen?... – Amadan Mar 23 '15 at 02:09
  • What do you mean by duplicates? Each on has different img fields, but other fields may be the same. Be more specific on what constitutes a duplicate. – Jonathan Wheeler Mar 23 '15 at 02:09
  • Apologizes all for any inconsistencies or omissions as i'm a lil new. The background is this is for a mobile app project in titanium. – osa1 Mar 23 '15 at 02:50
  • @nnnnnn Are you saying you want to remove items from the array if they are "duplicates" in the sense of having the same type, inkcolor and body as other items (since those are the three fields you use in your loop)? YES, thats what i want. – osa1 Mar 23 '15 at 03:04
  • @Amadan, Can you give the desired output? I want to get the 5 pen makers ; 3 or 4 pen types, 3 or 4 ink colors and the 5 or so body types. These values will be put in separate arrays that will feed apps table view row in titanium. /Users/osahon/Desktop/Screen Shot 2015-03-22 at 11.10.09 PM.png – osa1 Mar 23 '15 at 03:10

2 Answers2

0

Typically you want to sort your array and then if duplicates exist they will be next to each other in the array.

This question has been answered before though Removing duplicates in array of objects

Or instead of writing your own, you can leverage existing implementations like lodash's https://lodash.com/docs#uniq

Community
  • 1
  • 1
ken4z
  • 1,340
  • 1
  • 11
  • 18
0

If I understood your clarification in question comments, this should make the separate arrays of field values:

var makers = {},
    bodies = {},
    inkcolors = {},
    types = {};

for (var i = 0; i < importedData.data.length; i++) {
    var item = importedData.data[i];
    makers[item.maker] = true;
    bodies[item.body] = true;
    inkcolors[item.inkcolor] = true;
    types[item.type] = true;
};
makers = Object.keys(makers);
bodies = Object.keys(bodies);
inkcolors = Object.keys(inkcolors);
types = Object.keys(types);

console.log(makers);
console.log(bodies);
console.log(inkcolors);
console.log(types);

Explanation: Objects makers, bodies etc. are used as hashtables. When "Bic" maker is first encountered, makers will get a new attribute: { "Bic": true }. On "inkwell", we insert another one: { "Bic": true, "inkwell": true }. When we see "inkwell" again, we just reassign the existing attribute to true, so the object is unchanged. The value actually does not matter at all; I like true (basically saying "I saw Bic! I saw inkwell, too!"), but it could have been 17.5 for all it matters, or you could have kept running total ("I saw two Bics! I saw one inkwell!" { "Bic": 2, "inkwell": 1 }). This hinges on the fact that all keys in an object are unique. At the end, we just extract those keys from each object as an array: ["Bic", "inkwell", ...].

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks. Its giving me a error. saying that "Can't find variable: makers"; – osa1 Mar 23 '15 at 04:57
  • Ah, right, I forgot the initialisation. I'll paste it in presently. – Amadan Mar 23 '15 at 04:58
  • You the man. its showing me exactly what i wanted in my console. honestly..though if you could break down what u did i would appreciate it...Maybe specifically the object.keys part. Thanks again – osa1 Mar 23 '15 at 05:14