大家好,我想要从一个对象(我也作为参数给出)中删除一个条目(我作为参数给出)。为了清楚起见,我将为您提供一些代码。
这是我要从中删除条目的对象
public class RecyclerViewAdapter1 extends RecyclerView.Adapter<RecyclerViewHolder1> {
Esercenti esercenti;
ArrayList<User1> user1ArrayList;
public RecyclerViewAdapter1(Esercenti esercenti, ArrayList<User1> user1ArrayList) {
this.esercenti = esercenti;
this.user1ArrayList = user1ArrayList;
}
@NonNull
@Override
public RecyclerViewHolder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(esercenti.getBaseContext());
View view = layoutInflater.inflate(R.layout.single_rowesercenti, parent, false);
return new RecyclerViewHolder1(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder1 holder, int position) {
holder.mnomeEsercente.setText(user1ArrayList.get(position).getNomeEsercente());
holder.mindirizzoEsercente.setText(user1ArrayList.get(position).getIndirizzoEsercente());
}
@Override
public int getItemCount() {
return user1ArrayList.size();
}
}
我要删除的条目如下:public class Esercenti extends FragmentActivity implements OnMapReadyCallback {
FirebaseFirestore db1;
RecyclerView mRecyclerViewEsercenti;
ArrayList<User1> user1ArrayList;
GoogleMap map;
RecyclerViewAdapter1 adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_esercenti);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
user1ArrayList = new ArrayList<>();
setUpRecyclerView();
setUpFirebase();
loadDataFromFirebase();
}
private void loadDataFromFirebase() {
if (user1ArrayList.size() > 0)
user1ArrayList.clear();
db1.collection("esercenti").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot querySnapshot : task.getResult()) {
User1 user1 = new User1(querySnapshot.getString("nomeEsercente"), querySnapshot.getString("indirizzoEsercente"));
user1ArrayList.add(user1);
}
adapter1 = new RecyclerViewAdapter1(Esercenti.this, user1ArrayList);
mRecyclerViewEsercenti.setAdapter(adapter1);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Esercenti.this, "Problema di caricamento,controlla la tua connessione!", Toast.LENGTH_SHORT).show();
}
});
}
private void setUpFirebase() {
db1 = FirebaseFirestore.getInstance();
}
private void setUpRecyclerView() {
mRecyclerViewEsercenti = findViewById(R.id.RecyclerViewEsercenti);
mRecyclerViewEsercenti.setHasFixedSize(true);
mRecyclerViewEsercenti.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng base = new LatLng(0, 0);
map.addMarker(new MarkerOptions().position(base).title("Sede AirBc"));
map.animateCamera(CameraUpdateFactory.newLatLng(base));
map.animateCamera(CameraUpdateFactory.zoomIn());
}
}
然后我将调用像这样的函数const register = {
aap :[{condition: {aap:'aap'}, func: 'fun'}],
aap1 :[{condition: {aap1:'aap1'}, func: 'fun'},{condition: {aap2:'aap2'}, func: 'fun'}],
};
在上述情况下,从寄存器中删除给定的条件对象。
现在这是我自己建立的:
{aap:'aap'}
我想出了一些方法,可以用来从中删除:
myfunction(register,{aap:'aap'})
但是我仍然没有弄清楚如何删除与我在函数中提供的条目匹配的条目。
长话短说。我想要一个包含2个参数的函数。一个参数是寄存器(我想从中删除对象),第二个参数是我要删除的条件。
返回函数后,所需结果应为:
const register = {
aap :[{condition: {aap:'aap'}, func: 'fun'}],
aap1 :[{condition: {aap1:'aap1'}, func: 'fun'},{condition: {aap2:'aap2'}, func: 'fun'}],
};
function hallo(register,condition){
const objKeys = [];
for(key in register){
objKeys.push(key);
}
objKeys.forEach(function(el){
console.log(register[el]);
})
}
hallo(register);
寄存器减去我要删除的条件。
答案 0 :(得分:3)
您可以采用递归方法,迭代对象的所有键,然后检查该键是否在value
中,然后在对象中删除此键。
继续嵌套对象。
function remove(object, value) {
Object.keys(object).forEach(key => {
if (key in value && object[key] === value[key]) {
delete object[key];
return;
}
if (object[key] && typeof object[key] === 'object') {
remove(object[key], value);
}
});
return object;
}
const register = { aap: [{ condition: { aap: 'aap' }, func: 'fun' }], aap1: [{ condition: {aap1: 'aap1' }, func: 'fun' }, { condition: { aap2: 'aap2' }, func: 'fun' }] };
console.log(remove(register, { aap: 'aap' }));
.as-console-wrapper { max-height: 100% !important; top: 0; }