0
 comment =[   {
            "id": 4,
            "snippetId": 1,
            "text": "This code is correct but looks verbose. May be replace this hashset with an array so you can statically initialize?",
            "line": 30,
            "published_date": "2019-08-14T18:15:05.360241Z"
        },
        {
            "id": 11,
            "snippetId": 6,
            "text": "On first glance, the code seems to be fine. But it assumes that first two numbers are each single-digit numbers. I think you should attempt to modify this method to include the more general case where first two numbers are multiple digits.",
            "line": 4,
            "published_date": "2019-08-15T00:36:55Z"
        },
        {
            "id": 12,
            "snippetId": 6,
            "text": "indentation is off. Some interviewers may not like this.",
            "line": 36,
            "published_date": "2019-08-15T00:40:25.296813Z"
        }

]

//this is my json //please help how to filter the object who have snippetId = 6 (react js)

  • 3
    Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jun 14 '20 at 07:22

1 Answers1

-1

You should also study a bit more what a json is and how to use arrays in javascript though.

JSON.parse(comment).find(item => item.snippetId == 6)

comment = `[{
    "id": 4,
    "snippetId": 1,
    "text": "This code is correct but looks verbose. May be replace this hashset with an array so you can statically initialize?",
    "line": 30,
    "published_date": "2019-08-14T18:15:05.360241Z"
  },
  {
    "id": 11,
    "snippetId": 6,
    "text": "On first glance, the code seems to be fine. But it assumes that first two numbers are each single-digit numbers. I think you should attempt to modify this method to include the more general case where first two numbers are multiple digits.",
    "line": 4,
    "published_date": "2019-08-15T00:36:55Z"
  },
  {
    "id": 12,
    "snippetId": 6,
    "text": "indentation is off. Some interviewers may not like this.",
    "line": 36,
    "published_date": "2019-08-15T00:40:25.296813Z"
  }
]`;

console.log(JSON.parse(comment).find(item => item.snippetId == 6));
Balastrong
  • 4,336
  • 2
  • 12
  • 31
  • i am using the typescript with react and this code is giving error, if you have any solution please reply me – aman gupta Jun 14 '20 at 10:05
  • This code itself does not give any error actually. If you could let me know what error you get in your application I can try to help you. – Balastrong Jun 14 '20 at 10:13
  • ERROR in [at-loader] ./src/View/index.tsx:43:42 TS7006: Parameter 'item' implicitly has an 'any' type. i 「wdm」: Failed to compile. – aman gupta Jun 14 '20 at 10:24
  • Replace the find with `.find((item: any) => item.snippetId == 6));` – Balastrong Jun 14 '20 at 10:40
  • rgument of type 'Comment[]' is not assignable to parameter of type 'string'. – aman gupta Jun 14 '20 at 12:54
  • const createCommentWidgets = (cm: any) => { var myData = JSON.parse(comments); myData.find((item: any) => item.snippetId == 6) .map((comment :any ) => addCommentLineWidget(cm, comment)); }; – aman gupta Jun 14 '20 at 12:54