简体   繁体   English

在 Javascript 中使用部分字符串匹配过滤数组

[英]Filter Array Using Partial String Match in Javascript

I have an array of objects where the value I need to filter on is buried in a long string.我有一个对象数组,我需要过滤的值被埋在一个长字符串中。 Array looks like:数组看起来像:

                          {
                            "data": {
                              "value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}",
                              "partnerId": 1
                            }
                          },

So if I wanted to grab all the partnerId objects where value includes parent_sku how would I do that?因此,如果我想获取 value 包括parent_sku的所有partnerId对象,我该怎么做?

console.log(data.value.includes('parent_sku') returns cannot read property 'includes' of null . console.log(data.value.includes('parent_sku')返回cannot read property 'includes' of null

EDIT:编辑:

Didn't think this mattered, but judging by responses, seems it does.不认为这很重要,但从回应来看,似乎确实如此。 Here's the full response object:这是 object 的完整响应:

 Response body: {
                      "data": {
                        "configurationByCode": [
                          {
                            "data": {
                              "value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}",
                              "partnerId": 1
                            }
                          }

I'm passing that into a re-usable function for filtering arrays:我将其传递给可重复使用的 function 以过滤 arrays:

const parentSkuPartners = filterArray(res.body.data.configurationByCode, 'parent_sku');

Function: Function:

function filterArray(array, filterList) {
  const newList = [];
  for (let i = 0; i < array.length; i += 1) {
    console.log('LOG', array[i].data.value.includes('parent_sku');
   }
}

The problem is somewhere else.问题出在其他地方。 The code you've tried should work to find if a value contains a string – I've added it the snippet below and you'll see it works.您尝试过的代码应该可以查找值是否包含字符串——我已将其添加到下面的代码段中,您会看到它有效。

The issue is how you are accessing data and data.value .问题是您如何访问datadata.value The error message clearly states that it believes that data.value is null.错误消息清楚地表明它认为data.value是 null。 We would need to see the code around it to be able to figure out what the problem is.我们需要查看它周围的代码才能找出问题所在。 Try just logging to console the value of data before you run the includes function.在运行includes function 之前,尝试仅记录以控制台data值。

 const data = { "value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}", "partnerId": 1 }; console.log('includes?', data.value.includes('parent_sku'));

You can use data.value.includes('parent_sku') as you have suggested.您可以按照您的建议使用data.value.includes('parent_sku') The issue here is that your object is nested inside an unnamed object.这里的问题是您的 object 嵌套在未命名的 object 中。

try:尝试:

"data": {
    "value": "{\"cols\":[\"parent_sku\"],\"label\":\"Style\",\"description\":\"Enter Style.\",\"placeholderText\":\"Style 10110120103\"}",
    "partnerId": 1
}

The problem was some of the values for value were null .问题是 value 的一些valuenull Adding an extra conditional fixed it:添加一个额外的条件修复它:

if (array[i].data.value !== null) {

Use lodash includes, and lodash filter like使用 lodash 包含和 lodash 过滤器,例如

let configurationByCode = [{
    data: {
        value: {
            cols:["parent_sku"],
            label:"Style",
            description:"Enter Style.",
            placeholderText:"Style 10110120103"
        },
        "partnerId": 1
    }
}, {
    data: {
        value: {
            cols:["nothing"],
            label:"Style",
            description:"Enter Style.",
            placeholderText:"Style 10110120103"
        },
        "partnerId": 2
    }
}];

let wantedData =  _.filter(configurationByCode, (config) => { 
    return _.includes(config.data.value.cols, 'parent_sku'); 
});

console.log( wantedData );

https://jsfiddle.net/76cndsp2/ https://jsfiddle.net/76cndsp2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM