Вы можете перебрать массив и отклонить элементы в child_array
, которые вам не нужны. Обратите внимание, что это изменит исходный объект.
const obj = [{"father_id":1,"name":"father 1","child_array":[{"id":11,"name":"father 1 child 1"},{"id":12,"name":"father 1 child 2"}]},{"father_id":2,"name":"father 2","child_array":[{"child_id":21,"name":"father 2 child 1"},{"child_id":22,"name":"father 2 child 2 - TO DELETE"}]}];
_.forEach(obj, item => {
item.child_array = _.reject(item.child_array, {child_id: 22});
});
console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Мы могли бы помочь, если бы вы предоставили некоторые образцы данных. Сеть полна учебников по использованию следующих функций, которые делают то, что вас интересует:
gather()
и spread()
из пакета tidyr
melt()
и [114 ] из пакета reshape2
Вот подход, использующий tidyr
.
library(tidyr)
df2 <- df %>%
# (optional) First convert all the data columns to text so
# they're readable throughout the process.
mutate_at(vars(Date:Q6_text), as.character) %>%
# Gather into long format, where we record the column it came from
# as "question" and the value it held as "value"
gather(question, value, -Subject_ID, -row_num) %>%
# Combine the row_num and question into a new column
unite("question2", c("row_num", "question")) %>%
# Use that new column to spread everything out
spread(question2, value)
> df2
Subject_ID 1_Date 1_Q1 1_Q2_text 1_Q3 1_Q4 1_Q5 1_Q6_text 2_Date 2_Q1 2_Q2_text 2_Q3 2_Q4 2_Q5 2_Q6_text 3_Date 3_Q1 3_Q2_text 3_Q3 3_Q4 3_Q5 3_Q6_text
1 1 2019-01-01 4 Because 5 5 1 and so on 2019-01-02 1 O, bother 5 4 1 <NA> 2019-01-03 2 <NA> 3 4 <NA> <NA>
2 2 2018-12-04 <NA> <NA> 1 4 1 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
3 3 2018-12-15 3 In addtion 5 <NA> 3 <NA> 2018-12-26 1 <NA> 4 3 2 in conclusion <NA> <NA> <NA> <NA> <NA> <NA> <NA>