0

I have an array like below:

const data = [
{"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
{"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
{"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
{"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
...
]

What I want to do is this. Suppose, I only want to see the conversation between user1 and user 2.

So I want to filter the array and make a new array which will contain only those data where sender is user1 and receiver is user2 or sender is user2 and receiver is user1.

I think, it can be done using map or stuff like that but I actually have no idea. I am new to React Native.

1
  • Question authors are not obligated to "accept" an answer. However, we prefer it if an author does accept an answer, since this removes the question out of the unanswered lists and API feeds. You don't need to worry about favouritism when accepting - just choose one thought was most helpful or explanatory. Commented Jun 28, 2020 at 13:04

5 Answers 5

2
let result = []

getSenderRec = (user1, user2) => {
    data.filter(data => {
        if (user1 === data.sender && user2 === data.receiver || user2 === data.sender && user1 === data.receiver){
                result.push(data)
            }
        }   
    )
}
const data = [
{"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
{"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
{"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
{"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
]

getSenderRec('user1', 'user2')

console.log(result)

Here is your console.log(result) output:

[ { id: '1', sender: 'user1', receiver: 'user2', msg: 'hello!' },
  { id: '2', sender: 'user2', receiver: 'user1', msg: 'yo!' } ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the second quickest answer!! I am going to test it right now! I will vote up then :)
2

Use Array.filter. The callback returns a boolean:

const data = [
{"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
{"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
{"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
{"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
];

function filterData(data) {
    return data.filter((x) => (x.sender === 'user1' && x.receiver === 'user2') || (x.sender === 'user2' && x.receiver ===  'user2'));
}

const result = filterData(data);

2 Comments

Thanks :) Gonna test all and inform you all :D
snack.expo.io/@nothingtosay/courageous-orange this shows this worst error :(
1
You need just use filter for array

const data = [
{"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
{"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
{"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
{"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
]

const result = data.filter(rec => {
  if (rec.sender === 'user1' && rec.receiver === 'user2') {
    return rec
  }
  if (rec.sender === 'user2' && rec.receiver === 'user1') {
    return rec
  }
})

3 Comments

Thanks for the quickest reply! I am going to test it right now! :)
This shows Objects are not valid as react childs :(
this shows this worst error D: snack.expo.io/@nothingtosay/joyous-donut
1

You need to use like below

 const data = [
{"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
{"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
{"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
{"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
...
]

let filtereddata = data.filter((el)=>{
    return el.sender === "user1" && el.receiver === "user2"
    
})

here filtereddata will have only result which you wanted to show

7 Comments

Thanks!! Gonna test it and let you all know :)
This shows this horrible error: snack.expo.io/@nothingtosay/crabby-apple plz help :(
the code you have sent is class based component and you cann't declare your const variable like that either move it to componentDidMount() or any other function that need to be declared inside your react class component it will work
Anku I made some updates, please check it now. it still shows the same
You need to make a minor change when you are showing your data in render function you are printing the whole object i mean like this <Text>{this.state.data}</Text> but you need to extract some specific value from the array and print like {this.state.data[0].id}
|
1

You can maintain an array of users and based on that you can filter the array:

var data = [
{"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
{"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
{"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
{"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
];
var users=['user1', 'user2'];

var result = data.filter(chat=>users.includes(chat.sender) && users.includes(chat.receiver));

console.log(result);

Or You can also have an array of filters maintained for keys of the object. It will short the code even more. Here it is:

var data = [
    {"id":"1", "sender":"user1", "receiver":"user2", "msg":"hello!"},
    {"id":"2", "sender":"user2", "receiver":"user1", "msg":"yo!"},
    {"id":"3", "sender":"user3", "receiver":"user1", "msg":"hi!!"},
    {"id":"4", "sender":"user1", "receiver":"user3", "msg":"hmm"},
];

var users=['user1', 'user2'];
var filters=['sender', 'receiver'];

var result = data.filter(chat=>filters.every(k=>users.includes(chat[k])));
console.log(result);

1 Comment

Uh!! This community is awesome!! I gonna test all these three and let you all know my votes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.