Home > Software design >  How to check with whole raw object (without using any property) if that object contains in array of
How to check with whole raw object (without using any property) if that object contains in array of

Time:12-10

I want to check the entire raw object with array of objects. I dont want to compare by using any property name.

 const items = [{ a: 1 },{ a: 2 },{ a: 3 }];
  const item1 = { a: 2 };
  

What to do if i want to check if item1 exist in items array without using any property but with
whole object.
indexOf filter some and even lopping does not work on this condition.
Need help on this. How to check?
by the way, my condition is different. I have a long object and its uniquness depends upon lots of properties, that why I do not want to compare using properties.

I have something like this.

function bigArraySol() {
  const bigArray = [
    {
      product_id: "1",
      product_name: "Womens Achara",
      total_price: "8000",
      unique_product_id: "1",
      seller_id: "sell1",
      image: "https://i.imgur.com/6CsDtqD.jpg",
      quantity: 3,
      product_types: [
        {
          product_type_id: "1",
          product_type_name: "Achara Cloth",
          attributes: [
            {
              attribute_name: "Achara Length",
              attribute_value: "4"
            },
            {
              attribute_name: "Achara Cloth",
              attribute_value: "Terry Cotton"
            }
          ]
        },
        {
          product_type_id: "2",
          product_type_name: "Khadki ",
          attributes: [
            {
              attribute_name: "Khadki Cloth",
              attribute_value: "Ready Made"
            },
            {
              attribute_name: "khadki Color",
              attribute_value: "mix"
            }
          ]
        },
        {
          product_type_id: "3",
          product_type_name: "Blouse",
          attributes: [
            {
              attribute_name: "Blouse Size",
              attribute_value: "20"
            }
          ]
        }
      ]
    },
    {
      product_id: "1",
      product_name: "Womens Achara",
      total_price: "8000",
      unique_product_id: "1",
      seller_id: "sell1",
      image: "https://i.imgur.com/6CsDtqD.jpg",
      quantity: 3,
      product_types: [
        {
          product_type_id: "1",
          product_type_name: "Achara Cloth",
          attributes: [
            {
              attribute_name: "Achara Length",
              attribute_value: "4"
            },
            {
              attribute_name: "Achara Cloth",
              attribute_value: "Terry Cotton"
            }
          ]
        },
        {
          product_type_id: "2",
          product_type_name: "Khadki ",
          attributes: [
            {
              attribute_name: "Khadki Cloth",
              attribute_value: "Ready Made"
            },
            {
              attribute_name: "khadki Color",
              attribute_value: "mix"
            }
          ]
        },
        {
          product_type_id: "3",
          product_type_name: "Blouse",
          attributes: [
            {
              attribute_name: "Blouse Size",
              attribute_value: "25"
            }
          ]
        }
      ]
    },
    {
      product_id: "1",
      product_name: "Womens Achara",
      total_price: "8000",
      unique_product_id: "1",
      seller_id: "sell1",
      image: "https://i.imgur.com/6CsDtqD.jpg",
      quantity: 3,
      product_types: [
        {
          product_type_id: "1",
          product_type_name: "Achara Cloth",
          attributes: [
            {
              attribute_name: "Achara Length",
              attribute_value: "4"
            },
            {
              attribute_name: "Achara Cloth",
              attribute_value: "Terry Cotton"
            }
          ]
        },
        {
          product_type_id: "2",
          product_type_name: "Khadki ",
          attributes: [
            {
              attribute_name: "Khadki Cloth",
              attribute_value: "Ready Made"
            },
            {
              attribute_name: "khadki Color",
              attribute_value: "mix"
            }
          ]
        },
        {
          product_type_id: "3",
          product_type_name: "Blouse",
          attributes: [
            {
              attribute_name: "Blouse Size",
              attribute_value: "25"
            }
          ]
        }
      ]
    }
  ];

  const compairingObj = {
    product_id: "1",
    product_name: "Womens Achara",
    total_price: "8000",
    unique_product_id: "1",
    seller_id: "sell1",
    image: "https://i.imgur.com/6CsDtqD.jpg",
    quantity: 3,
    product_types: [
      {
        product_type_id: "1",
        product_type_name: "Achara Cloth",
        attributes: [
          {
            attribute_name: "Achara Length",
            attribute_value: "4"
          },
          {
            attribute_name: "Achara Cloth",
            attribute_value: "Terry Cotton"
          }
        ]
      },
      {
        product_type_id: "2",
        product_type_name: "Khadki ",
        attributes: [
          {
            attribute_name: "Khadki Cloth",
            attribute_value: "Ready Made"
          },
          {
            attribute_name: "khadki Color",
            attribute_value: "mix"
          }
        ]
      },
      {
        product_type_id: "3",
        product_type_name: "Blouse",
        attributes: [
          {
            attribute_name: "Blouse Size",
            attribute_value: "20"
          }
        ]
      }
    ]
  };
}

see in sandbox: https://codesandbox.io/s/javascript-forked-q8yu6?file=/index.js:0-4933

CodePudding user response:

You can simply JSON stringify them and compare.

const items = [{ a: 1 },{ a: 2 },{ a: 3 }];
const item1 = { a: 2 };

items.map((item, index) => {
  if (JSON.stringify(item) === JSON.stringify(item1)) {
    console.log('item1 exists in index '   index);
  }
})

CodePudding user response:

This code uses loops, and looks at properties, to implement an "object-in-array indexOf". It is very simple, and will match any object in the haystack that has the same values for each of the properties of the needle object. It should be easy to modify to check that the match does not have properties not found in the needle - but I am unsure of your needs.

const indexOf = (needle, haystack) => {
  let index = -1;
  haystack.every((o,i) => {
    let found = true;
    for (let [k,v] of Object.entries(needle)) {
      if (o[k] !== v) { found = false; break; } 
    }
    if (found) index=i;
    return !found; // false exits
  });
  return index;
}

const haystack = [{ a: 1 },{ a: 2 },{ a: 3 }];
const needle = { a: 2 };

console.log(indexOf(needle, haystack)); // returns 1

  • Related