Home > Enterprise >  Find object in json array Using javascript
Find object in json array Using javascript

Time:08-04

I am trying to find the index of an object containing (From:) inside a JSON array so I did a recursive function to find the From: text and i used Lodash and FindIndex function but it is not working as expected and it is always returning -1

My code:

    // item is the actual object in the json array
   // arr is the parent holding item 
  // mdfdoc is the whole json array
 static handleText(item, mdfdoc, arr) {
    try {
        let theText = item.type === "text" ? item.text : item.content.text
        if (theText && theText.replace(/\s/g, '') != "") {
                if (theText === "From:") {
                    const index = _.findIndex(mdfdoc, arr);
                    console.log(index);
                }
        }
    }
    catch(ex){console.log(ex)}
}

object:

{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": "From:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    },
    {
      "type": "text",
      "text": " Bassem Alameddine <[email protected]> "
    },
    {
      "type": "hardBreak"
    },
    {
      "type": "text",
      "text": "Sent:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    },
    {
      "type": "text",
      "text": " Tuesday, August 2, 2022 12:28 PM"
    },
    {
      "type": "hardBreak"
    },
    {
      "type": "text",
      "text": "To:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    },
    {
      "type": "text",
      "text": " Bassem Alameddine <[email protected]>"
    },
    {
      "type": "hardBreak"
    },
    {
      "type": "text",
      "text": "Subject:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    }
  ]
},

Full Array:

{
  "type": "doc",
  "version": 1,
  "content": [
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": "Testing",
      "marks": [
        {
          "type": "em"
        },
        {
          "type": "strong"
        }
      ]
    }
  ]
},
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": "dadas",
      "marks": [
        {
          "type": "em"
        },
        {
          "type": "strong"
        },
        {
          "type": "textColor",
          "attrs": {
            "color": "#ff0000"
          }
        }
      ]
    }
  ]
},
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": " "
    }
  ]
},
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": "From:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    },
    {
      "type": "text",
      "text": " Bassem Alameddine <[email protected]> "
    },
    {
      "type": "hardBreak"
    },
    {
      "type": "text",
      "text": "Sent:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    },
    {
      "type": "text",
      "text": " Tuesday, August 2, 2022 12:28 PM"
    },
    {
      "type": "hardBreak"
    },
    {
      "type": "text",
      "text": "To:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    },
    {
      "type": "text",
      "text": " Bassem Alameddine <[email protected]>"
    },
    {
      "type": "hardBreak"
    },
    {
      "type": "text",
      "text": "Subject:",
      "marks": [
        {
          "type": "strong"
        }
      ]
    }
  ]
},
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": " "
    }
  ]
}
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": " "
    }
  ]
},
{
  "type": "paragraph",
  "content": [
    {
      "type": "text",
      "text": " "
    }
  ]
}
 ]
}

CodePudding user response:

// if more than one index occurs it will return an array of indexes
const isTextHasFrom = (text) => text === "From:";
const getArrayOfIndex = (acc, item, index) => {
  const getText = item.type === "text" ? item.text : item?.content;
  if (typeof getText === "string") {
    if (isTextHasFrom(getText)) {
      acc.push(index);
    }
  } else {
    if (item.content.some(({ text }) => isTextHasFrom(text))) {
      acc.push(index);
    }
  }
  return acc;
};

const indexes = data.reduce(getArrayOfIndex, []);
console.log(indexes);

CodePudding user response:

I wrote simple recurssive function that return firstIndex that has From:

const findFromArr = (obj, index = 0) => {
    if (obj.type === "text") {
        return obj.text.includes("From:") ? index : false
    } else {
        const index = obj.content.findIndex((obj, index) => {
            const result = findFromArr(obj, index)
            return result
        })
        return index;
    }

}

you can simply your whole json to this function.

CodePudding user response:

use this function it will search you the key if it is in object

function searchIndexByKey(arr, key){
 var re = -1;
    for(var p=0;p<arr.length;p  ){
    var obj = arr[p];
    var res= searchObjectByKey(obj, key, p);
    if(res>=0){
      re = res;
      break;
    }
  }
  if(re>=0){
    return re;
  }else{
   return -1;
  }
}

function searchObjectByKey(obj, k, index) {
  
for (var key in obj) {
    var value = obj[key];

    if (k == key) {
      return index;
      break;
    }

    if (typeof(value) === "object" && !Array.isArray(value)) {
      var y = searchObjectByKey(value, k, index);
      if (y && y[0] == k) return y;
    }
    if (Array.isArray(value)) {
      
      for (var i = 0; i < value.length;   i) {
        var x = searchObjectByKey(value[i], k, index);
        if (x && x[0] == k) return x;
      }
    }
  }
  return -1;
}

var arr = [
{
  "type": "doc",
  "version": 1,
},
{
  "type1": "doc",
  "version1": 1,
}];
  
console.log(searchIndexByKey(arr,'type'));
  • Related