首页 文章

基于多列数据框中的单元格创建新列,类似于字符串

提问于
浏览
0

我知道所有的行都有一个类似于我感兴趣的字符串的值 . 但是,我不知道它将在哪个列中 . 我想创建一个包含值的新列 .

例如,取下面的数据框,我想要字符串“ap”

V1  V2
1   apple  orange
2   banana apricot
3   grape  apple

基于此我想创建第三个变量V3:

V3
1    apple
2    apricot
3    apple

1 回答

  • 0

    我假设你的变量是一个字符串数组 .

    var V1 = ["apple", "banana", "grape"];
    var V2 = ["orange", "apricot", "apple"];
    
    var V3 = [];
    
    V1.forEach(function (element) {
        if (element.indexOf("ap") === 0) {
            V3.push(element);
        }
    });
    
    V2.forEach(function (element) {
        if (element.indexOf("ap") === 0) {
            V3.push(element);
        }
    });
    

相关问题