如何在ExtJS中将数据存入数组并忽略日期提取指定属性
Hey there! Let's walk through how to handle these two ExtJS tasks clearly and practically:
1. 将推送数据存入ExtJS数组
You’ve got two solid options here, depending on whether you need a simple array or something tied to ExtJS’s data layer for UI components:
Option 1: Plain JavaScript Array (for basic storage)
Start with an empty array, then push incoming push data into it whenever it arrives (e.g., from a WebSocket or server-sent event):
// Initialize empty array let pushDataArray = []; // Handler function for incoming push data function handlePushData(newData) { // Add the new data entry to the array pushDataArray.push(newData); // Optional: Verify the update in console console.log('Updated array:', pushDataArray); }
Option 2: ExtJS ArrayStore (for UI binding)
If you need to connect this data to ExtJS components like grids or forms, use Ext.data.ArrayStore—it’s built on arrays but adds sorting, filtering, and binding capabilities:
// Create an ArrayStore with fields matching your data structure let pushStore = Ext.create('Ext.data.ArrayStore', { fields: ['fieldA', 'fieldB'], // Replace with your actual field names data: [] // Start with empty data }); // When push data comes in function handlePushData(newRecord) { // Add the record to the store (internally managed as an array) pushStore.add(newRecord); // Optional: Sync with server if needed pushStore.sync(); }
2. 提取response中的属性1.1、1.2并忽略日期存入数组
Let’s assume your console.log(response) outputs a structure like this:
// Example response object const response = { "prop1_1": "user_123", "prop1_2": "active", "date": "2024-05-20T14:22:00", "otherIrrelevantProp": "ignore_me" };
Here’s how to grab only the properties you need and skip the date entirely:
Step 1: Set up your target array
let filteredDataArray = [];
Step 2: Process the response and push to array
Use object destructuring to pull only prop1_1 and prop1_2 (adjust the property names to match your actual response keys), then push the filtered object to your array:
// Extract only the properties we care about const { prop1_1, prop1_2 } = response; // Create a clean object without the date or extra fields const cleanEntry = { prop1_1: prop1_1, prop1_2: prop1_2 }; // Add to the target array filteredDataArray.push(cleanEntry); // Check the result console.log('Filtered array:', filteredDataArray);
Pro tip: If you’re dealing with an array of response objects (multiple records), loop through them with forEach to process all entries at once:
const responseList = [ { prop1_1: "user_456", prop1_2: "inactive", date: "2024-05-19" }, { prop1_1: "user_789", prop1_2: "pending", date: "2024-05-20" } ]; responseList.forEach(item => { const { prop1_1, prop1_2 } = item; filteredDataArray.push({ prop1_1, prop1_2 }); });
内容的提问来源于stack exchange,提问作者Shiromi

