[英]How to return all array items in array before a specific key
I'm needing to return items from an array.我需要从数组中返回项目。 The difficulty is that arrays returned from a fetch request have the possibility of returning different keys that can't be determined prior to the return.
困难在于从获取请求返回的 arrays 有可能返回在返回之前无法确定的不同键。 The common denominator is the 'created_by' key.
共同点是“created_by”键。 As the title suggests, I need to know a way that can render all the previous values, prior to the created_by key, dynamically.
正如标题所暗示的那样,我需要知道一种可以在 created_by 键之前动态呈现所有先前值的方法。
This is the render/map method I have setup now that will not work as it stands.这是我现在设置的渲染/映射方法,它不会像现在这样工作。
return <div className="data-subContainer">
{resData.map(item => <span key={item.id}>{item[1.3]} - <b>{item[2]}</b></span>)}
</div>
resData Ouput: resData 输出:
(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {4: "X", 5: "X", 6: "05/16/2020", id: "4627", form_id: "3", post_id: null, date_created: "2020-05-16 05:27:50", date_updated: "2020-05-16 05:27:50", …}
1: {4: "X", 5: "X", 6: "05/14/2020", id: "4618", form_id: "3", post_id: null, date_created: "2020-05-14 13:50:32", date_updated: "2020-05-14 13:50:32", …}
2: {4: "X", 5: "X", 6: "03/02/2020", id: "4124", form_id: "3", post_id: null, date_created: "2020-03-02 19:01:18", date_updated: "2020-03-02 19:01:18", …}
3: {4: "X", 5: "X", 6: "02/27/2020", id: "4103", form_id: "3", post_id: null, date_created: "2020-02-27 00:38:54", date_updated: "2020-02-27 00:38:54", …}
4: {4: "X", 5: "X", 6: "02/27/2020", id: "4102", form_id: "3", post_id: null, date_created: "2020-02-27 00:38:14", date_updated: "2020-02-27 00:38:14", …}
5: {4: "X", 5: "X", 6: "02/27/2020", id: "4101", form_id: "3", post_id: null, date_created: "2020-02-27 00:34:44", date_updated: "2020-02-27 00:34:44", …}
6: {4: "X", 5: "X", 6: "01/14/2020", id: "3853", form_id: "3", post_id: null, date_created: "2020-01-14 07:37:23", date_updated: "2020-01-14 07:37:23", …}
7: {4: "X", 5: "X", 6: "11/06/2019", id: "3503", form_id: "3", post_id: null, date_created: "2019-11-06 12:35:04", date_updated: "2019-11-06 12:35:04", …}
8: {4: "X", 5: "X", 6: "09/26/2019", id: "3283", form_id: "3", post_id: null, date_created: "2019-09-26 17:52:06", date_updated: "2019-09-26 17:52:06", …}
9: {4: "X", 5: "X", 6: "09/24/2019", id: "3263", form_id: "3", post_id: null, date_created: "2019-09-24 20:08:35", date_updated: "2019-09-24 20:08:35", …}
length: 10
__proto__: Array(0)
Example 1: (Notice key numbers are different than example 2)示例 1:(注意键编号与示例 2 不同)
1: "XX"
2: "XXXX"
3.2: ""
3.3: "XX"
3.4: ""
3.6: "X"
3.8: ""
4: "X"
5: "X"
8: "X"
10: "X"
11: "X"
12: "X"
13: "X"
created_by: "X"
Example 2:示例 2:
1.2: ""
1.3: "X"
1.4: ""
1.6: "X"
1.8: ""
4: "X"
5: "X"
6: "X"
7: "X"
8: "X"
9: "X"
created_by: "X"
You can try to cast to Map您可以尝试转换为 Map
var resMap = new Map(Object.entries(resData));
Then you can normally iterate via keys然后您通常可以通过键进行迭代
resMap.keys()
UPDATED ANSWER更新的答案
const resData = {
1: 'XX',
2: 'XXXX',
3.2: '',
3.3: 'XX',
3.4: '',
3.6: 'X',
3.8: '',
4: 'X',
5: 'X',
8: 'X',
10: 'X',
11: 'X',
12: 'X',
13: 'X',
created_by: 'X',
test: 1
};
const resMap = new Map(Object.entries(resData));
for (const [key, value] of resMap) {
if (key === 'created_by') { return; }
console.log(key, value);
}
You can use for in
to loop through the object, push
the values to a new object, then break
after created_by
is reached.您可以使用
for in
循环遍历 object,将值push
送到新的 object,然后在达到created_by
后break
。
var arr = {1: "XX", 2: "XXXX", 3.2: "", 3.3: "XX", 3.4: "", 3.6: "X", 3.8: "", 4: "X", 5: "X", 8: "X", 10: "X", 11: "X", 12: "X", 13: "X", created_by: "X", test: 1 }; var newa = {}; for(i in arr){ newa[i] = arr[i]; if(i == "created_by"){ break; } } console.log(newa);
If that data is modelled as an object, it seems so from the keys, you do not have a definite chance to know in which order the keys are since JavaScript objects does not guarantee the order, unlike arrays.如果该数据被建模为 object,从键看来,您没有明确的机会知道键的顺序,因为 JavaScript 对象不保证顺序,这与 ZA3CBC3F9D0CE2F2C1554E1B671ZD 不同。
If it is in your hands to retrieve this data as an array you could have the order guaranteed, and I would suggest you accordingly.如果您可以将这些数据作为数组检索,则可以保证顺序,我会相应地建议您。 Similar to the following hypothetical case's way.
类似于以下假设案例的方式。
You can an algorithm that accepts the names of the props value of which you want to render or not to render.您可以使用一种算法来接受要渲染或不渲染的道具值的名称。
First, you need a filter algorithm for objects like that:首先,您需要一个过滤器算法来处理这样的对象:
function filterObject(object, filteringFunction, initialAccumulator = {}) {
return Object.entries(object)
.filter(([key, value], indexInEntriesArray, entriesArray) => filteringFunction(key, value, indexInEntriesArray, entriesArray))
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), initialAccumulator);
}
Having that in hand you can filter the keys from an object based on a custom filtering function you defined.有了它,您可以根据您定义的自定义过滤 function 从 object 过滤密钥。 You can do the filtering according to anything you want based on key, value, index in entries array (returned form the
Object.entries
call).您可以根据条目数组中的键、值、索引(从
Object.entries
调用返回)根据您想要的任何内容进行过滤。
After that you need to map your resData
array itself with that function, performing the filtering props(s) of each object as you see fit:之后,您需要使用 function 对您的
resData
数组本身进行 map,按照您认为合适的方式执行每个 object 的过滤道具:
const mappedResData = resData.map(item => filterObject(item, myFilteringFunc))
with your custom filteringFunction
to filter each object inside the array.使用您的自定义
filteringFunction
来过滤阵列内的每个 object。 You can do anything here.你可以在这里做任何事情。 Here I just eliminate the key(s) that you list.
在这里,我只是消除了您列出的键。
const myFilteringFunc = (key, val, i, entArr) => undesiredKeysArray.includes(key) === false;
where undesiredKeysArray
is just an array of strings each of which is a property's name in each object in resData
.其中
undesiredKeysArray
只是一个字符串数组,每个字符串都是 resData 中每个resData
中的属性名称。
If order in JavaScript objects were guaranteed, which is not the case, you could Object
static methods keys
, values
or entries
to get and array of those and look for the name of the key you want to stop rendering at and slice
that array at there, and render the return of that slice
call.如果 JavaScript 对象中的顺序得到保证,但事实并非如此,您可以
Object
slice
方法keys
、 values
或entries
在您想要在其中停止呈现的键、值或条目的名称和数组,并渲染该slice
调用的返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.