我有两个对象,我需要根据另一个对象的属性匹配向一个对象添加属性:
"visible":false,
"type": 'hidden',
我想补充一下:
tableCols
到data
对象dependsOn
属性与 public class MainActivity extends AppCompatActivity {
private LinearLayoutManager linearLayoutManager;
private Adapter adap;
private EditText dialogButton;
private Context context =this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(linearLayoutManager);
List<Students> list = getListItemData();
adap = new Adapter(getApplicationContext(), list);
rv.setAdapter(adap);
dialogButton = (EditText) findViewById(R.id.et_amount);
dialogButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final android.app.AlertDialog.Builder inputAlert = new android.app.AlertDialog.Builder(context);
inputAlert.setTitle("Title of the Input Box");
inputAlert.setMessage("We need your name to proceed");
final EditText userInput = new EditText(context);
inputAlert.setView(userInput);
inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String userInputValue = userInput.getText().toString();
EditText et=(EditText)findViewById(R.id.et_amount);
et.setText(userInputValue);
}
});
inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
android.app.AlertDialog alertDialog = inputAlert.create();
alertDialog.show();
}
});
}
public List<Students> getListItemData(){
List<Students> listItem = new ArrayList<Students>();
listItem.add(new Students("Prashant Dhimal",850));
listItem.add(new Students("Prashanta",850));
listItem.add(new Students("Pratistha",850));
listItem.add(new Students("Prince",850));
listItem.add(new Students("Ranjan",850));
listItem.add(new Students("Rabi Tiwari",850));
listItem.add(new Students("Rajan ",850));
listItem.add(new Students("Rahul",850));
listItem.add(new Students("Rachana",850));
listItem.add(new Students("Ravi karki",850));
listItem.add(new Students("Raisha",850));
listItem.add(new Students("Rashmi",850));
listItem.add(new Students("Rinku",850));
listItem.add(new Students("Rajat",850));
listItem.add(new Students("Rabin",850));
listItem.add(new Students("Ramesh",850));
listItem.add(new Students("Ragbendra",850));
listItem.add(new Students("Subash Gautam",850));
listItem.add(new Students("Subash Gc",850));
listItem.add(new Students("Subash Parajuli",850));
listItem.add(new Students("Sudip bajgai",850));
listItem.add(new Students("Suman Thapa",850));
listItem.add(new Students("Suman Shrestha",850));
listItem.add(new Students("Saurav Man Pradhan",850));
listItem.add(new Students("Saurav Karn",850));
listItem.add(new Students("Shruti",850));
listItem.add(new Students("Swagat Gaire",850));
listItem.add(new Students("Swagat Ranjit",850));
listItem.add(new Students("Swikriti ",850));
listItem.add(new Students("Udin",850));
listItem.add(new Students("Ujwal",850));
listItem.add(new Students("Yunisha",850));
listItem.add(new Students("Sahan",850));
listItem.add(new Students("Samyukta",850));
listItem.add(new Students("Sanjay Koju",850));
listItem.add(new Students("Sanjay Thapa",850));
listItem.add(new Students("Srizan",850));
listItem.add(new Students("Ram",850));
listItem.add(new Students("Susovik",850));
listItem.add(new Students("Suraj",850));
listItem.add(new Students("Ujwal",850));
listItem.add(new Students("Sital",850));
listItem.add(new Students("Siddharth",850));
listItem.add(new Students("Sagar",850));
listItem.add(new Students("Sudarshan",850));
return listItem;
}
}
中的属性匹配。
答案 0 :(得分:0)
使用Object.keys
和Array.indexOf
函数的解决方案:
var dependsOn = {
qTableIdent: {'RHID': 'RHID', 'SEQ': 'NOME'},
qTableDocs: {'RHID': 'RHID', 'DOC': 'DOC2'}
},
tableCols = [
{
"targets": 1,
"title": 'RHID',
"label": 'RHID',
"data": 'RHID',
"name": 'RHID',
"width": "5%",
"filter": true,
"defaultContent": "",
attr: {'name': 'rhid'}
}
],
newProps = {"visible": false, "type": 'hidden'}, // new properties to insert
dataKeys = [];
Object.keys(dependsOn).forEach(function(k) {
Object.keys(dependsOn[k]).forEach(function(key) {
if (dataKeys.indexOf(key) === -1) {
dataKeys.push(key);
}
});
});
// dataKeys now contains unique keys list: ["RHID", "SEQ", "DOC"]
tableCols.forEach(function(obj) {
if (dataKeys.indexOf(obj['data']) !== -1) {
Object.keys(this).forEach((k) => obj[k] = this[k]);
}
}, newProps);
console.log(JSON.stringify(tableCols, 0, 4));
console.log(JSON.stringify(tableCols, 0 , 4));
'tableCols'数组中单个对象的输出示例:
[
{
"targets": 1,
"title": "RHID",
"label": "RHID",
"data": "RHID",
"name": "RHID",
"width": "5%",
"filter": true,
"defaultContent": "",
"attr": {
"name": "rhid"
},
"visible": false,
"type": "hidden"
}
]